2017-05-28 156 views
0

我有一个数据帧列表。每个数据帧只有一列,数据分隔为“\ t”。不同数据帧的列名称不同。我想拆分列并为新列提供名称。数据帧列表的拆分列

list.df <- c(df1,df2,df3....df1000) 
df1 
$`000004.ame` 
[1] 0.0\t0.00\t12.1\t1.0\t14.01\t1  2.0\t0.00\t13.9\t-0.2\t14.02\t1  
[3] 4.0\t-0.00\t13.2\t0.2\t14.01\t1  6.0\t0.00\t12.8\t0.0\t14.02\t1  
[5] 8.0\t0.00\t13.7\t0.0\t14.02\t1 

other data frames are similar with different column names 

我得到了它的一个数据帧,但我想申请数据的列表框

colnames(X) <- "text" 
library(splitstackshape) 
X <- cSplit(as.data.frame(X),"text","\t") 
colnames(X) <- c("T","I") 

如何申请这个数据帧的列表? 请指引我对此

+0

请分享您的数据 – Sotos

+0

我编辑我的问题 – user821

+1

使用'lapply'遍历列表,像'lapply(list.df,功能(我的一个重复的例子){ (未测试) – Sotos

回答

0
library(stringr) 

# Creating similiar dfs 

vec1 <- c("0.0\t0.00\t12.1\t1.0\t14.01\t1","0.0\t0.00\t12.1\t1.0\t14.01\t1" 
,"0.0\t0.00\t12.1\t1.0\t14.01\t1","0.0\t0.00\t12.1\t1.0\t14.01\t1") 
df1 <- as.data.frame(vec1, stringsAsFactors = FALSE) 

df2 <- df1 

list.df <- c(df1,df2) 

# Looping over this list with lapply 
# Using stringrs str_split instead of splitstackshape 


list.splitted.dfs <- lapply(list.df, function(x) unlist(str_split(x[1], 
"\t"))) 

# Output from above is a list, need to bind it together 
new <- as.data.frame(do.call(rbind, list.splitted.dfs)) 
> newdf 
V1 V2 V3 V4 V5 V6 
1 0.0 0.00 12.1 1.0 14.01 1 
2 0.0 0.00 12.1 1.0 14.01 1