来自字符向量的数据框,每行包含三个逗号分隔值

时间:2016-08-13 01:21:53

标签: r

如何从每个行中包含三个逗号分隔值的长字符向量创建数据框的列。第一个元素包含数据框列的名称。

并非每一行都有三列,有些地方只有一个逗号:

> 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,我只对前两列感兴趣并且会抛出第三列。

R is awesome

这是原始字符串,有些列不是逗号转义但这不是一个大问题。

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..."

3 个答案:

答案 0 :(得分:3)

您是否尝试过read.csv的文字输入?

df <- read.csv( text = string.split.cols, header = T )

答案 1 :(得分:1)

我发现这个例程非常快,可以分割字符串并转换为数据帧。

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是要拆分的字符串向量。

答案 2 :(得分:1)

分割字符串后,您可以使用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]]
)