R - 读取包含带逗号字符串的列的文件

时间:2016-06-18 04:15:16

标签: r

我试图读取一个包含带逗号字符串的列的文件,有点像这样:

Input <- "A, B, C
1.76, 1%, yei, yei
2.00, 2%, foo
3.000, 3% , foo"

其中:yei,yei是colum C的值:

A      B        C
1.76   1%   yei, yei
2.00   2%      test
3.000  3%      foo

我认为转换功能可能有效,可以在R中处理这种情况吗?

2 个答案:

答案 0 :(得分:2)

我们可以使用read.table

Input <- read.table(text="A, B, C
1.76, 1%, 'yei, yei'
2.00, 2%, foo
3.000, 3% , foo", sep=",", header=TRUE, stringsAsFactors=FALSE)

Input
#    A  B        C
#1 1.76 1% yei, yei
#2 2.00 2%      foo
#3 3.00 3%      foo

更新

如果&#39; yei,yei&#39;没有引号,我们可以在用sub

阅读后用readLines创建一个引号
Input <- readLines(textConnection("A, B, C
1.76, 1%, yei, yei
2.00, 2%, foo
3.000, 3% , foo"))

 read.table(text=sub("(([^,]+,){2})\\s+(.*)", "\\1'\\3'", Input), 
             header=TRUE, sep=",", stringsAsFactors=FALSE)
 #     A    B        C
 #1 1.76   1% yei, yei
 #2 2.00   2%      foo
 #3 3.00  3%       foo

答案 1 :(得分:1)

A <- c(1.76, 2.00, 3.000)
B <- c("1%", "2%", "3%")
C <- c("yei, yei", "foo", "foo")

as.data.frame(cbind(A,B,C))

#     A  B        C
#1 1.76 1% yei, yei
#2    2 2%      foo
#3    3 3%      foo