Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

split dataframe in R by row

Tags:

r

I have a long dataframe like this:

  Row  Conc   group
  1     2.5    A
  2     3.0    A
  3     4.6    B
  4     5.0    B
  5     3.2    C
  6     4.2    C
  7     5.3    D
  8     3.4    D

...

The actual data have hundreds of row. I would like to split A to C, and D. I looked up the web and found several solutions but not applicable to my case.

How to split a data frame?

For example: Case 1:

x = data.frame(num = 1:26, let = letters, LET = LETTERS)
set.seed(10)
split(x, sample(rep(1:2, 13)))

I don't want to split by arbitrary number

Case 2: Split by level/factor

data2 <- data[data$sum_points == 2500, ]

I don't want to split by a single factor either. Sometimes I want to combine many levels together.

Case 3: select by row number

newdf <- mydf[1:3,]

The actual data have hundreds of rows. I don't know the row number. I just know the level I would like to split at.

like image 771
Amateur Avatar asked Oct 29 '12 16:10

Amateur


1 Answers

It sounds like you want two data frames, where one has (A,B,C) in it and one has just D. In that case you could do

Data1 <- subset(Data, group %in% c("A","B","C"))
Data2 <- subset(Data, group=="D")

Correct me if you were asking something different

like image 167
Señor O Avatar answered Sep 21 '22 01:09

Señor O