How to split a data frame in R with conditional row values?

R ProgrammingServer Side ProgrammingProgramming

The splitting of data frame is mainly done to compare different parts of that data frame but this splitting is based on some condition and this condition can be row values as well. For example, if we have a data frame df where a column represents categorical data then the splitting based on the categories can be done by using subset function as shown in the below examples.

Example1

Consider the below data frame:

Live Demo

> Country<-rep(c("India","China","Russia","Sudan"),5)
> Ratings<-sample(1:5,20,replace=TRUE)
> df1<-data.frame(Country,Ratings)
> df1

Output

 Country Ratings
1 India   1
2 China   2
3 Russia  5
4 Sudan   3
5 India   5
6 China   5
7 Russia  5
8 Sudan   5
9 India   2
10 China  1
11 Russia 5
12 Sudan  4
13 India  3
14 China  1
15 Russia 1
16 Sudan  2
17 India  3
18 China  4
19 Russia 5
20 Sudan  2

Splitting df1 for groups (India-China, Russia, and Sudan):

Example

> C1<-subset(df1,Country %in% c("India","China"))
> C1

Output

  Country Ratings
1 India    1
2 China    2
5 India    5
6 China    5
9 India    2
10 China   1
13 India   3
14 China   1
17 India   3
18 China   4

Example

> C2<-subset(df1,Country %in% c("Russia"))
> C2

Output

  Country Ratings
3 Russia    5
7 Russia    5
11 Russia   5
15 Russia   1
19 Russia   5

Example

> C3<-subset(df1,Country %in% c("Sudan"))
> C3

Output

 Country Ratings
4 Sudan    3
8 Sudan    5
12 Sudan   4
16 Sudan   2
20 Sudan   2

Example2

Consider the below data frame:

Live Demo

> Season<-sample(c("Summer","Spring","Winter"),20,replace=TRUE)
> Rain<-sample(c("Yes","No"),20,replace=TRUE)
> df2<-data.frame(Season,Rain)
> df2

Output

  Season Rain
1 Spring  Yes
2 Winter  Yes
3 Spring  Yes
4 Spring   No
5 Winter   No
6 Summer   No
7 Summer   No
8 Winter  Yes
9 Winter  Yes
10 Winter Yes
11 Summer Yes
12 Summer Yes
13 Summer Yes
14 Summer  No
15 Winter  No
16 Spring  No
17 Summer Yes
18 Spring Yes
19 Winter  No
20 Winter  No

Splitting df2 for Winter, Summer, and Spring separately:

Example

> S1<-subset(df2,Season %in% c("Winter"))
> S1

Output

  Season Rain
2 Winter  Yes
5 Winter   No
8 Winter  Yes
9 Winter  Yes
10 Winter Yes
15 Winter  No
19 Winter  No
20 Winter  No

Example

> S2<-subset(df2,Season %in% c("Summer"))
> S2

Output

  Season  Rain
6 Summer   No
7 Summer   No
11 Summer Yes
12 Summer Yes
13 Summer Yes
14 Summer  No
17 Summer Yes

Example

> S3<-subset(df2,Season %in% c("Spring"))
> S3

Output

  Season  Rain
1 Spring  Yes
3 Spring  Yes
4 Spring   No
16 Spring  No
18 Spring Yes
raja
Published on 23-Nov-2020 09:51:31
Advertisements