Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
173 views
in Technique[技术] by (71.8m points)

r - Split data.frame by value

how can I split the following data.frame

df <- data.frame(var1 = c("a", 1, 2, 3, "a", 1, 2, 3, 4, 5, 6, "a", 1, 2), var2 = 1:14)

into lists of / groups of

a 1
1 2
2 3
3 4

a 5
1 6
2 7
3 8
4 9
5 10
6 11

a 12
1 13
2 14

So basically, value "a" in column 1 is the tag / identifier I want to split the data frame on. I know about the split function but that means I have to add another column and since, as can be seen from my example, the size of the groups can vary I do not know how to automatically create such a dummy column to fit my needs.

Any ideas on that?

Cheers,

Sven

Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You could find which values of the indexing vector equal "a", then create a grouping variable based on that and then use split.

df[,1] == "a"
# [1]  TRUE FALSE FALSE FALSE  TRUE FALSE FALSE FALSE FALSE FALSE FALSE  TRUE
#[13] FALSE FALSE
cumsum(df[,1] == "a")
# [1] 1 1 1 1 2 2 2 2 2 2 2 3 3 3
split(df, cumsum(df[,1] == "a"))
#$`1`
#  var1 var2
#1    a    1
#2    1    2
#3    2    3
#4    3    4
#
#$`2`
#   var1 var2
#5     a    5
#6     1    6
#7     2    7
#8     3    8
#9     4    9
#10    5   10
#11    6   11
#
#$`3`
#   var1 var2
#12    a   12
#13    1   13
#14    2   14

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...