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.
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