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 Convert a List to a Dataframe
Example 1: Convert List to Dataframe in R
# create a list
list1 <- list(A = c("Sabby", "Cathy", "Dormy"),
B = c(18, 24, 22),
C = c("Computer Science", "Engineering", "Business")
)
# convert list to dataframe
result <- as.data.frame(list1)
print(result)
Output
A B C
1 Sabby 18 Computer Science
2 Cathy 24 Engineering
3 Dormy 22 Business
In the above example, we have used the as.data.frame()
function to convert the list named list1 to dataframe.
First, the names of the list A
, B
, C
are converted to dataframe columns and finally all the list elements are added in each column respectively.
Example 2: Change Column Names While Convert to R Dataframe
In R, we pass the col.names
parameter to change the name of the column. For example,
# create a list
list1 <- list(A = c("Sabby", "Cathy", "Dormy"),
B = c(18, 24, 22),
C = c("Computer Science", "Engineering", "Business")
)
# convert list to dataframe and change name of column
result <- as.data.frame(list1,
col.names = c("Name", "Age", "Course")
)
print(result)
Output
Name Age Course
1 Sabby 18 Computer Science
2 Cathy 24 Engineering
3 Dormy 22 Business
In the above example, we have passed the col.names
parameter to the as.data.frame()
function to change the name of each column.
result <- as.data.frame(list1,
col.names = c("Name", "Age", "Course")
)
Here, the list name
A
in changed to"Name"
B
in changed to"Age"
C
in changed to"Course"