我\xc2\xb4m 尝试创建一个数据框,其中包含一个ID 和一个数字向量,每行的成绩数未知,但我不\xc2\xb4t 知道如何执行此操作。
\nstudentmean <-\n data.frame(\n Student = character(),\n Grades = c(0),\n stringsAsFactors = FALSE\n )\n
Run Code Online (Sandbox Code Playgroud)\n后来我尝试使用以下方法向数据框中添加新行:
\ngradeList <- getGradesOfStudent(data$ResultFrame, matriculationNumber)$Grades\n studentmean[nrow(studentmean) + 1, ] = list(as.character(matriculationNumber), gradeList\n
Run Code Online (Sandbox Code Playgroud)\n如何在单个数据框单元格内存储数值向量?
\n我同意 Stephen Henderson 的评论,即您不应该使用列表列,除非您绝对确定它们是解决您的特定问题的最佳方法。话虽这么说,如果您确实决定使用列表列,您可能需要考虑使用 tibbles 而不是数据框。Tibbles 是常规数据框的“升级”。它们是 tidyverse 的一部分并包含在tibble
包装中。
Tibbles 可以轻松创建列表列:
tibble(x = 1:3, y = list(1:5, 1:10, 1:20))
#> # A tibble: 3 x 2
#> x y
#> <int> <list>
#> 1 1 <int [5]>
#> 2 2 <int [10]>
#> 3 3 <int [20]>
Run Code Online (Sandbox Code Playgroud)
nest
此外,您可以使用命令和包unnest
中的“打包”和“解包”列表列tidyr
。例如:
df <- tibble(
x = 1:3,
y = c("a", "d,e,f", "g,h")
)
df %>%
transform(y = strsplit(y, ",")) %>%
unnest(y)
Run Code Online (Sandbox Code Playgroud)
有关 tibbles 的更多信息,您可以查阅此小插图。