[英]Delimit R dataframe column ignoring commas in quote
I am trying to split a data frame column delimiting on stand-alone commas but ignoring commas in single quotes.我正在尝试拆分以独立逗号分隔但忽略单引号中的逗号的数据框列。 I have tried several things but can't get any to work.
我已经尝试了几件事,但无法正常工作。
Here is a simplified version of df
:这是
df
的简化版本:
V1
'914061', 'Palo Alto'
'930061', 'Brooklyn, New York, USA'
Desired output:期望的输出:
V1, V2
914061,Palo Alto
930061, Brooklyn, New York, USA (location in one cell)
I tried this but it split up Brooklyn and New York and USA (and returned a warning about vector length)我试过了,但它把布鲁克林、纽约和美国分开了(并返回了关于向量长度的警告)
df2 <- data.frame(do.call('rbind',strsplit(as.character(df$V1),',',fixed=TRUE)))
I also tried this but get "Error in file(file, "rt") : invalid 'description' argument"我也试过这个但得到“文件错误(文件,“rt”):无效的“描述”参数”
df2 <- read.delim(df$V1, header=F, sep=',', dec = '.', stringsAsFactors=F , quote = "\"" , fill = TRUE)
您在应该使用quote="'"
quote='"'
地方使用了quote='"'
。并强制read.csv
将两列作为变量读取,而不是将一列转换为行名。
read.csv(df$V1, quote="'", row.names=NULL)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.