如何从每行包含三个逗号分隔值的长字符向量创建数据框列。第一个元素包含数据框列的名称。
并不是每一行都有三列,有些地方只有一个逗号:
> string.split.cols[1] #This row is the .names
[1] "Acronym,Full form,Remarks"
> string.split.cols[2]
[1] "AC,Actual Cost, "
> string.split.cols[3]
[1] "ACWP,Actual Cost of Work Performed,Old term for AC"
> string.split.cols[4]
[1] "ADM,Arrow Diagramming Method,Rarely used now"
> string.split.cols[5]
[1] "ADR,Alternative Dispute Resolution, "
> string.split.cols[6]
[1] "AE,Apportioned Effort, "输出应该是包含三列的df,我只对前两列感兴趣,并将抛出第三列。

这是原始字符串,有些列没有逗号转义,但这不是什么大问题。
string.cols <- [1] "Acronym,Full form,Remarks\nAC,Actual Cost, \nACWP,Actual Cost of Work Performed,Old term for AC\nADM,Arrow Diagramming Method,Rarely used now\nADR,Alternative Dispute Resolution, \nAE,Apportioned Effort, \nAOA,Activity-on-Arrow,Rarely used now\nAON,Activity-on-Node, \nARMA,Autoregressive Moving Average, \nBAC,Budget at Completion, \nBARF,Bought-into, Approved, Realistic, Formal,from Rita Mulcahy's PMP Exam Prep\nBCR,Benefit Cost Ratio, \nBCWP,Budgeted Cost of Work Performed,Old term for EV\nBCWS,Budgeted Cost of Work Scheduled,Old term for PV\nCA,Control Account, \nCBR,Cost Benefit Ratio, \nCBT,Computer-Based Test, \n..."
发布于 2016-08-13 09:29:13
您尝试过read.csv的文本输入吗
df <- read.csv( text = string.split.cols, header = T )发布于 2016-08-13 09:29:36
我发现这个例程对于拆分字符串和转换为数据帧非常快。
slist<-strsplit(mylist,",")
x<-sapply(slist, FUN= function(x) {x[1]})
y<-sapply(slist, FUN= function(x) {x[2]})
df<-data.frame(Column1Name=x, Column2Name=y, stringsAsFactors = FALSE)其中mylist是要拆分的字符串向量。
发布于 2016-08-13 09:47:52
拆分字符串后,可以使用rbind.data.frame执行此操作:
x <- do.call(rbind.data.frame, strsplit(split.string.cols[-1], ','))
names(x) <- strsplit(split.string.cols[1], ',')[[1]]
x
## Acronym Full form Remarks
## 1 AC Actual Cost
## 2 ACWP Actual Cost of Work Performed Old term for AC
## ...作为一行程序:
setNames(do.call(rbind.data.frame,
strsplit(split.string.cols[-1], ',')
),
strsplit(split.string.cols[1], ',')[[1]]
)https://stackoverflow.com/questions/38928311
复制相似问题