如何一次修改嵌套数据的每个数据集的所有列?
I have this nested data
我想解开它,但是我必须标准化列的类,以便
`library(tidyverse`)
nested_data<-iris %>% nest(data = !Species)
#I added to the third dataset an additionnal variable
nested_data$data[[3]]$randomVar<-round(rnorm(nrow(
nested_data$data[[3]]),100,5),1)
#I dropped a column of the second dataset
nested_data$data[[2]]$Sepal.Length<-NULL
#I changed the type of certain variables
nested_data$data[[2]]$Petal.Length<- as.character(
nested_data$data[[2]]$Petal.Length)
nested_data$data[[1]]$Petal.Width<-as.character(
nested_data$data[[1]]$Petal.Width
)
用不同类型的类别的某些变量的类别我无法解开
nested_data%>%unnest(data)
我的错误消息:
Error: Can't combine `..1$Petal.Length` <double> and `..2$Petal.Length` <character>.
Run `rlang::last_error()` to see where the error occurred.
我想更改contract
使用loop 或任何vectorization
方法,使用使用
方法,三个数据集中每个数据集的所有变量。
我不知道该怎么做。
I have this nested data
I want to unnest it, but I have to standardize the classes of the columns before to unnest
`library(tidyverse`)
nested_data<-iris %>% nest(data = !Species)
#I added to the third dataset an additionnal variable
nested_data$data[[3]]$randomVar<-round(rnorm(nrow(
nested_data$data[[3]]),100,5),1)
#I dropped a column of the second dataset
nested_data$data[[2]]$Sepal.Length<-NULL
#I changed the type of certain variables
nested_data$data[[2]]$Petal.Length<- as.character(
nested_data$data[[2]]$Petal.Length)
nested_data$data[[1]]$Petal.Width<-as.character(
nested_data$data[[1]]$Petal.Width
)
With different type of classes for certain variables I can not unnest
nested_data%>%unnest(data)
I have this error message:
Error: Can't combine `..1$Petal.Length` <double> and `..2$Petal.Length` <character>.
Run `rlang::last_error()` to see where the error occurred.
I want to change in character
all the variables of each of the three datasets in one line of codes using a for loop
or any vectorization
method.
I have no idea how to do it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果列类型是偶然的,则可以在
type.convert
之前使用unnest
output
或
type.convert.convert
将无法使用(由于字符元素,然后将列迫使列contrac
,unnest
,然后使用type.convert.convert
更改列类型If the column types are different accidentally, then can use
type.convert
before theunnest
-output
Or if
type.convert
wouldn't work (because of character elements, then force the columns to be of typecharacter
,unnest
and then change the column types withtype.convert