![]() |
VOOZH | about |
In this article, we will discuss how to split the dataframe in R programming language.
A subset can be split both continuously as well as randomly based on rows and columns. The rows and columns of the dataframe can be referenced using the indexes as well as names. Multiple rows and columns can be referred using the c() method in base R.
The dataframe cells can be referenced using the row and column names and indexes.
Syntax:
data-frame[start-row-num:end-row-num,]
The row numbers are retained in the final output dataframe.
Example: Splitting dataframe by row
Output:
[1] "Original DataFrame" col1 col2 col3 1 Grp1 1 1 2 Grp1 2 2 3 Grp2 3 1 4 Grp2 1 2 5 Grp3 2 1 6 Grp3 3 2 [1] "Modified DataFrame" col1 col2 col3 1 Grp1 1 1 2 Grp1 2 2 3 Grp2 3 1 4 Grp2 1 2
Example: Splitting dataframe by row
Output:
[1] "Original DataFrame" col1 col2 col3 1 Grp1 1 1 2 Grp1 2 2 3 Grp2 3 1 4 Grp2 1 2 5 Grp3 2 1 6 Grp3 3 2 [1] "Modified DataFrame" col1 col2 col3 6 Grp3 3 2
The dataframe rows can also be generated randomly by using the set.seed() method. It generates a random sample, which is then fed into any arbitrary random dummy generator function. The rows can then be extracted by comparing them to a function.
Example: Splitting dataframe by rows randomly
Output:
[1] "Original DataFrame" col1 col2 col3 col4 1 Grp1 1 1 a 2 Grp1 2 2 b 3 Grp2 3 1 c 4 Grp2 1 2 d 5 Grp3 2 1 e 6 Grp3 3 2 f [1] "Modified DataFrame" col1 col2 col3 col4 5 Grp3 2 1 e 6 Grp3 3 2 f
The dataframe can also be referenced using the column names. Multiple column names can be specified using the c() method containing column names as strings. The column names may be contiguous or random in nature.
Syntax:
data-frame[,c(col1, col2,...)]
Example: splitting dataframe by column names
Output:
[1] "Original DataFrame" col1 col2 col3 col4 1 Grp1 1 1 a 2 Grp1 2 2 b 3 Grp2 3 1 c 4 Grp2 1 2 d 5 Grp3 2 1 e 6 Grp3 3 2 f [1] "Modified DataFrame" col2 col4 1 1 a 2 2 b 3 3 c 4 1 d 5 2 e 6 3 f
The dataframe can also be referenced using the column indices. Individual, as well as multiple columns, can be extracted from the dataframe by specifying the column position.
Syntax:
data-frame[,start-col-num:end-col-num]
Example: Split dataframe by column indices
Output:
[1] "Original DataFrame" col1 col2 col3 col4 1 Grp1 1 1 a 2 Grp1 2 2 b 3 Grp2 3 1 c 4 Grp2 1 2 d 5 Grp3 2 1 e 6 Grp3 3 2 f [1] "Modified DataFrame" col3 col4 1 1 a 2 2 b 3 1 c 4 2 d 5 1 e 6 2 f