将列表列表转换为 R 中的数据框
在本文中,我们将讨论如何在 R 编程语言中将列表列表转换为数据框。我们将按行和按列将列表列表转换为数据框。
示例 1: R 程序在具有数字和字符类型的列表中创建三个列表,并按列转换为数据帧。
Syntax as.data.frame(do.call(cbind, list_name))
Parameters: Where cbind is to convert list to dataframe by column and list_name is the input list which is list of lists
代码:
R
# create list and create 3 lists
# inside this list
lists = list(list1 = list(1, 2, 3, 4, 5),
list2 = list('a', 'b', 'c', 'd', 'e'),
list3 = list(2, 3, 4, 5, 6))
# convert list of lists into dataframe
# by column
print(as.data.frame(do.call(cbind, lists)))
R
# create list and create 2 lists
# inside this list
lists = list(list1 = list(1, 2, 3, 4, 5),
list2 = list('a', 'b', 'c', 'd', 'e'))
# convert list of lists into
# dataframe by column
print(as.data.frame(do.call(cbind, lists)))
R
# create list and create 3 lists
# inside this list
lists = list(list1 = list(1, 2, 3, 4, 5),
list2 = list('a', 'b', 'c', 'd', 'e'),
list3 = list(2, 3, 4, 5, 6))
# convert list of lists into dataframe
# by row
print(as.data.frame(do.call(rbind, lists)))
输出:
list1 list2 list3
1 1 a 2
2 2 b 3
3 3 c 4
4 4 d 5
5 5 e 6
示例 2: R 程序在具有数字和字符类型的列表中创建两个列表,并按列转换为数据框
电阻
# create list and create 2 lists
# inside this list
lists = list(list1 = list(1, 2, 3, 4, 5),
list2 = list('a', 'b', 'c', 'd', 'e'))
# convert list of lists into
# dataframe by column
print(as.data.frame(do.call(cbind, lists)))
输出:
list1 list2
1 1 a
2 2 b
3 3 c
4 4 d
5 5 e
示例 3: R 程序在具有数字和字符类型的列表中创建三个列表,并按列转换为数据帧。
Syntax: as.data.frame(do.call(rbind,list_name))
Parameters: Where rbind is to convert list to dataframe by row and list_name is the input list which is list of lists
电阻
# create list and create 3 lists
# inside this list
lists = list(list1 = list(1, 2, 3, 4, 5),
list2 = list('a', 'b', 'c', 'd', 'e'),
list3 = list(2, 3, 4, 5, 6))
# convert list of lists into dataframe
# by row
print(as.data.frame(do.call(rbind, lists)))
输出:
V1 V2 V3 V4 V5
list1 1 2 3 4 5
list2 a b c d e
list3 2 3 4 5 6