Course
Data Frames
Convert a List to a DataframeCreate an Empty DataframeCombine Two Dataframe into OneChange Column Name of a DataframeExtract Columns From a DataframeDrop Columns in a DataframeReorder Columns in a DataframeSplit DataframeMerge Multiple DataframesDelete Rows From DataframeMake a List of DataframesIntroduction
"Hello World" ProgramAdd Two VectorsFind Sum, Mean and Product of Vector in R ProgrammingTake Input From UserGenerate Random Number from Standard DistributionsSample from a PopulationFind Minimum and MaximumSort a VectorStrings
Concatenate Two StringsFind the Length of a StringCheck if Characters are Present in a StringExtract n Characters From a StringReplace Characters in a StringCompare two StringsConvert Factors to CharactersTrim Leading and Trailing WhitespacesVectors
Concatenate a Vector of StringsCheck if a Vector Contains the Given ElementCount the Number of Elements in a VectorFind Index of an Element in a VectorAccess Values in a VectorAdd Leading Zeros to VectorR Program to Split Dataframe
Example 1: Split Dataframe by Row Indexes in R
# Create a data frame
dataframe1 <- data.frame (
Name = c("Juan", "Alcaraz", "Simantha"),
Age = c(22, 15, 19),
Address = c("Nepal", "USA", "Germany")
)
# extract 1st row
print(dataframe1[1, ])
# extract 1st and 3rd row
print(dataframe1[c(1,3), ])
Output
Name Age Address
1 Juan 22 Nepal
Name Age Address
1 Juan 22 Nepal
3 Simantha 19 Germany
Here,
dataframe1[1, ]
- splits entire elements of 1st rowdataframe1[c(1,3), ]
- splits entire elements of 1st and 3rd row
Example 2: Split Dataframe by Column Names in R
# Create a data frame
dataframe1 <- data.frame (
Name = c("Juan", "Alcaraz", "Simantha"),
Age = c(22, 15, 19),
Address = c("Nepal", "USA", "Germany")
)
# extract 1st column
print(dataframe1[, "Name"])
# extract 1st and 3rd column
print(dataframe1[, c("Name", "Address")])
Output
[1] "Juan" "Alcaraz" "Simantha"
Name Address
1 Juan Nepal
2 Alcaraz USA
3 Simantha Germany
Here,
dataframe1[,"Name"]
- splits entire elements of 1st columndataframe1[, c("Name","Address")]
- splits entire elements of 1st and 3rd column
Note: Instead of column names we can also split data frame using column indexes as: [, 1]
and [, C(1,3)]
. The output will be the same.