- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to split a data frame in R with conditional row values?
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:
> 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:
> 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
- Related Articles
- How to split a data frame using row number in R?
- How to multiply row values in a data frame having multiple rows with single row data frame in R?
- How to divide data frame row values by row variance in R?
- How to replace missing values with row means in an R data frame?
- How to divide the row values by row mean in R data frame?
- How to divide the data frame row values in R by row median?
- How to divide the row values by row sum in R data frame?
- How to unsplit a split in R data frame?
- How to change row values based on column values in an R data frame?
- How to split a data frame by column in R?
- How to find the proportion of row values in an R data frame?
- How to subset row values based on columns name in R data frame?
- How to fill the NA values from above row values in an R data frame?
- How to divide data frame row values by maximum value in each row excluding 0 in R?
- How to add a row to a frame from another data frame in R?

Advertisements