0

I want to split the dataset into 2 in R based upon values in a column

My data is like this(example)

Sn  no   State
1   22   ALL
2   33   BALL
3   44   FALL
4   55   ALL
5   77   MALL
6   11   FALL

I want to split it into 2 data sets based upon a variable say A=40 which splits the dataset by checking in the 2nd column(one dataset having values greater than 40 the other lesser than 40). The output will be like :-

1   24   ALL
2   35   BALL
6   11   FALL

and

3   44   FALL
4   55   ALL
5   77   MALL
Evan Strom
  • 65
  • 1
  • 7

2 Answers2

1

Let's say you have a dataframe df having columns(Sn,no,State), you can split on second column using :

 df[df$no > 40, ]
adjustedR2
  • 161
  • 4
0

This questions looks a more specific version of the following, check out that thread for a detailed look at the split() function akrun recommended.

Split data.frame based on levels of a factor into new data.frames

To return two separate dataframes in the exact manner described above, I would recommend looking at subsetting.


library(tidyverse)

df <- tibble(
        no = c(22, 33, 44, 55, 77, 11),
        State = c('ALL', 'BALL', 'FALL', 'ALL', 'MALL', 'FALL')) %>%
        mutate(Sn = row_number())

glimpse(df)
Observations: 6
Variables: 3
$ no    <dbl> 22, 33, 44, 55, 77, 11
$ State <chr> "ALL", "BALL", "FALL", "ALL", "MALL", "FALL"
$ Sn    <int> 1, 2, 3, 4, 5, 6


df_greatertthan40 <- subset(df, no > 40)
df2_lessthan40 <- subset(df, no < 40)

There are many options for subsetting / splitting, check out the subsetting chapter in Advanced R for other options: http://adv-r.had.co.nz/Subsetting.html

JFlynn
  • 344
  • 2
  • 8