简体   繁体   English

分隔 R 数据框列忽略引号中的逗号

[英]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.

相关问题 根据2个字符分隔R中的一列 - Delimit a column in R based on 2 characters R - 逗号分隔一列,同时添加其他列 - R - Comma delimit a column while adding others 在 R 数据框中的两个逗号之间剪切一长列字符 - Cut a long column of characters between two commas in an R dataframe 查找忽略-Inf和NA的R数据框列的最大值 - Finding the max of a R dataframe column ignoring -Inf and NA 计算 R 中 dataframe 列的平均值,忽略 NA 和 0 值 - calculate the mean for a column of a dataframe in R ignoring NA and 0 values R - 用r中的逗号检查列索引 - R - check column index with commas in r R:将长单列dataframe展开成两列,按字母和数字拆分,忽略标点符号 - R: Spread long single column dataframe into two columns, spliting by alpha and numeric, ignoring punctuation 如何从文本文件中读取某些行,同时忽略使用 R 之间的几行(还将这些行分隔成列)? - How to read certain lines from a text files while ignoring few lines in between using R (also delimit those lines into columns)? 在数据集点r的列中转换逗号 - convert commas in a column of a data set points r 如何使用 R 将数据帧/矩阵写入 CSV,但值中有逗号 - How to write the dataframe/matrix into CSV using R but with commas in values
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM