如何修复使用 'nest' 导致列表列的列表?
How to fix the use of 'nest' to cause a list of list column?
我正在使用包含一些信息的基础数据框,该信息用于生成一个新列,即数据框。
我尝试使用 map2 函数,但结果并不好,使用 nest 函数和包 purrr 中的 tibble 的结果更接近我的预期。
library(tidyverse)
library(dplyr)
library(purrr)
library(RcppRoll)
dfBase <- data.frame(id = sample(letters[1:3], 3, replace = TRUE),
dat1 = rnorm(n = 3, 1, 1),
dat2 = rnorm(n = 3, 2, 1))
View(dfBase)
dfOperations <- function(dat1, dat2){
Sum = dat1+dat2
Rest = dat1-dat2
Mult = dat1*dat2
Div = dat1/dat2
test <- tibble(
Opera = c('Sum','Rest','Mult','Div'),
Resul = c(Sum, Rest, Mult, Div))%>%nest()
}
for (x in 1:nrow(dfBase)) {
dfBase$Result[x] <- dfOperations(dfBase$dat1[x],dfBase$dat2[x])
}
dfBase
str(dfBase)
View(dfBase$Result)
生成新列时,它会创建为列表列表而不是元素列表。
'data.frame': 3 obs. of 4 variables:
$ id : Factor w/ 2 levels "b","c": 1 1 2
$ dat1 : num 0.53 1.29 1.19
$ dat2 : num 0.915 2.844 1.052
$ Result:List of 3
..$ :List of 1
.. ..$ :Classes ‘tbl_df’, ‘tbl’ and 'data.frame': 4 obs. of 2 variables:
.. .. ..$ Opera: chr "Sum" "Rest" "Mult" "Div"
.. .. ..$ Resul: num 1.444 -0.385 0.484 0.579
..$ :List of 1
.. ..$ :Classes ‘tbl_df’, ‘tbl’ and 'data.frame': 4 obs. of 2 variables:
.. .. ..$ Opera: chr "Sum" "Rest" "Mult" "Div"
.. .. ..$ Resul: num 4.136 -1.552 3.675 0.454
..$ :List of 1
.. ..$ :Classes ‘tbl_df’, ‘tbl’ and 'data.frame': 4 obs. of 2 variables:
.. .. ..$ Opera: chr "Sum" "Rest" "Mult" "Div"
.. .. ..$ Resul: num 2.246 0.143 1.256 1.136
如何删除或消除列表 1,只留下第二个列表 Opera 和 Resul?
尝试在每个循环迭代中使用 [[1]]
访问 dfOperations
的输出:
for (x in 1:nrow(dfBase)) {
dfBase$Result[x] <- dfOperations(dfBase$dat1[x],dfBase$dat2[x])[[1]]
}
'data.frame': 3 obs. of 4 variables:
$ id : Factor w/ 3 levels "a","b","c": 1 3 2
$ dat1 : num 0.849 2.659 0.862
$ dat2 : num 2.45 1.15 1.35
$ Result:List of 3
..$ :Classes ‘tbl_df’, ‘tbl’ and 'data.frame': 4 obs. of 2 variables:
.. ..$ Opera: chr "Sum" "Rest" "Mult" "Div"
.. ..$ Resul: num 3.301 -1.603 2.082 0.346
..$ :Classes ‘tbl_df’, ‘tbl’ and 'data.frame': 4 obs. of 2 variables:
.. ..$ Opera: chr "Sum" "Rest" "Mult" "Div"
.. ..$ Resul: num 3.81 1.51 3.06 2.31
..$ :Classes ‘tbl_df’, ‘tbl’ and 'data.frame': 4 obs. of 2 variables:
.. ..$ Opera: chr "Sum" "Rest" "Mult" "Div"
.. ..$ Resul: num 2.208 -0.484 1.16 0.64
我正在使用包含一些信息的基础数据框,该信息用于生成一个新列,即数据框。
我尝试使用 map2 函数,但结果并不好,使用 nest 函数和包 purrr 中的 tibble 的结果更接近我的预期。
library(tidyverse)
library(dplyr)
library(purrr)
library(RcppRoll)
dfBase <- data.frame(id = sample(letters[1:3], 3, replace = TRUE),
dat1 = rnorm(n = 3, 1, 1),
dat2 = rnorm(n = 3, 2, 1))
View(dfBase)
dfOperations <- function(dat1, dat2){
Sum = dat1+dat2
Rest = dat1-dat2
Mult = dat1*dat2
Div = dat1/dat2
test <- tibble(
Opera = c('Sum','Rest','Mult','Div'),
Resul = c(Sum, Rest, Mult, Div))%>%nest()
}
for (x in 1:nrow(dfBase)) {
dfBase$Result[x] <- dfOperations(dfBase$dat1[x],dfBase$dat2[x])
}
dfBase
str(dfBase)
View(dfBase$Result)
生成新列时,它会创建为列表列表而不是元素列表。
'data.frame': 3 obs. of 4 variables:
$ id : Factor w/ 2 levels "b","c": 1 1 2
$ dat1 : num 0.53 1.29 1.19
$ dat2 : num 0.915 2.844 1.052
$ Result:List of 3
..$ :List of 1
.. ..$ :Classes ‘tbl_df’, ‘tbl’ and 'data.frame': 4 obs. of 2 variables:
.. .. ..$ Opera: chr "Sum" "Rest" "Mult" "Div"
.. .. ..$ Resul: num 1.444 -0.385 0.484 0.579
..$ :List of 1
.. ..$ :Classes ‘tbl_df’, ‘tbl’ and 'data.frame': 4 obs. of 2 variables:
.. .. ..$ Opera: chr "Sum" "Rest" "Mult" "Div"
.. .. ..$ Resul: num 4.136 -1.552 3.675 0.454
..$ :List of 1
.. ..$ :Classes ‘tbl_df’, ‘tbl’ and 'data.frame': 4 obs. of 2 variables:
.. .. ..$ Opera: chr "Sum" "Rest" "Mult" "Div"
.. .. ..$ Resul: num 2.246 0.143 1.256 1.136
如何删除或消除列表 1,只留下第二个列表 Opera 和 Resul?
尝试在每个循环迭代中使用 [[1]]
访问 dfOperations
的输出:
for (x in 1:nrow(dfBase)) {
dfBase$Result[x] <- dfOperations(dfBase$dat1[x],dfBase$dat2[x])[[1]]
}
'data.frame': 3 obs. of 4 variables:
$ id : Factor w/ 3 levels "a","b","c": 1 3 2
$ dat1 : num 0.849 2.659 0.862
$ dat2 : num 2.45 1.15 1.35
$ Result:List of 3
..$ :Classes ‘tbl_df’, ‘tbl’ and 'data.frame': 4 obs. of 2 variables:
.. ..$ Opera: chr "Sum" "Rest" "Mult" "Div"
.. ..$ Resul: num 3.301 -1.603 2.082 0.346
..$ :Classes ‘tbl_df’, ‘tbl’ and 'data.frame': 4 obs. of 2 variables:
.. ..$ Opera: chr "Sum" "Rest" "Mult" "Div"
.. ..$ Resul: num 3.81 1.51 3.06 2.31
..$ :Classes ‘tbl_df’, ‘tbl’ and 'data.frame': 4 obs. of 2 variables:
.. ..$ Opera: chr "Sum" "Rest" "Mult" "Div"
.. ..$ Resul: num 2.208 -0.484 1.16 0.64