R:读取具有多个分隔符和括号的表?

问题描述 投票:0回答:1

我已经收到这种类型的表,available also here

enter image description here

我想知道如何在R中有效地打开表?

我的输出应分为3个单独的列,并且不带括号::

      id   type  V1  
1 13242924 'SA'  1 
2 13035909 'SA'  1 
3 6685553  'SA'  1 
4 12990163 'SA'  1  

目前,我正在考虑将其拆分为几个步骤:

  • 使用\t分隔符以.csv格式打开文件,
  • 使用多个gsub()替换两个括号,>>
  • 将第一列分成两部分,等等。>
  • 没有更简单的方法吗?另外,似乎optim$V1 <- gsub('(', "", optim$V1)并不能完全消除括号。

df<- read.csv("C:/sample.csv",
               sep =  "\t",
               header = F)

# Replace the parantheses:
optim$V1 <- gsub('(', "", optim$V1)

我已经收到了这种类型的表,在这里我也想知道如何在R中有效地打开表?我的输出应分为3个单独的列,并且不带括号::id ...

r
1个回答
0
投票

你在这里,

library(dplyr)

(d <- tibble(id = c("(123","(24"),
            type = c("'sa')", "'sa')")))

d %>% mutate_at(vars(id, type), ~str_remove_all(.x, pattern = "\\(|\\)"))
© www.soinside.com 2019 - 2024. All rights reserved.