Separate data in R by line

I have a long data format:

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 

...

Actual data has hundreds of rows. I would like to split A into C and D. I looked on the Internet and found some 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 do not want to break into an arbitrary number

Case 2: Divide by Level / Ratio

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

I also do not want to share one factor. Sometimes I want to combine many levels.

Case 3: selection by line number

 newdf <- mydf[1:3,] 

Actual data contains hundreds of rows. I do not know the line number. I just know the level that I would like to break.

+4
source share
2 answers

It looks like you need two data frames in which one (A,B,C) and one has only D In this case, you could do

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

Correct me if you ask for something else

+4
source

You can use the recode() function in the car package.

 # Load the library and make up some sample data library(car) set.seed(1) dat <- data.frame(Row = 1:100, Conc = runif(100, 0, 10), group = sample(LETTERS[1:10], 100, replace = TRUE)) 

Currently, dat$group contains uppercase letters from A to J. Imagine that we need the following four groups:

  • "one" = A, B, C
  • "two" = D, E, J
  • "three" = F, I
  • "four" = G, H

Now use recode() (note the semicolon and enclosed quotation marks).

 recodes <- recode(dat$group, 'c("A", "B", "C") = "one"; c("D", "E", "J") = "two"; c("F", "I") = "three"; c("G", "H") = "four"') split(dat, recodes) 
0
source

Source: https://habr.com/ru/post/1442807/


All Articles