循环函数将行转换为列,直到空行

时间:2018-02-07 08:13:51

标签: r

我有数据被网页刮掉了,我的格式面临着严峻的挑战,我正在尝试解决这个问题并取得了很大的成功,这里是数据的外观

df<-as.data.frame(c("aa","ab","ac"," ","ba","bb","bc"," ","ca","cb","cc"," ","da","db","dc"))

我想要做的是将每个集转置到列的空白值,并使数据框看起来像这样,

a<-as.data.frame(c("aa", "ab", "ac"))
b<-as.data.frame(c("ba", "bb", "cc"))
c<-as.data.frame(c("ca", "cb", "cc"))
d<-as.data.frame(c("da", "db", "dc"))
df.new<-as.data.frame(c(a,b,c,d))
t(df.new)

2 个答案:

答案 0 :(得分:0)

我们创建了一个分组变量cumsumsplit列和rbind

i1 <- df[,1]==" "
m1 <- do.call(rbind, split(as.character(df[,1])[!i1], cumsum(i1)[!i1]))
as.data.frame(m1, stringsAsFactors = FALSE)
#  V1 V2 V3
#0 aa ab ac
#1 ba bb bc
#2 ca cb cc
#3 da db dc

答案 1 :(得分:0)

x=as.character(df$a)
y=!grepl("\\s",x)
z=ave(y,cumsum(!y),FUN=cumsum)
unstack(stack(by(x,z,as.character)[-1]))
  X1 X2 X3
1 aa ab ac
2 ba bb bc
3 ca cb cc
4 da db dc