如何一次修改嵌套数据的每个数据集的所有列?

发布于 2025-01-24 19:04:57 字数 1060 浏览 0 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

可是我不能没有你 2025-01-31 19:04:57

如果列类型是偶然的,则可以在type.convert之前使用unnest

library(dplyr)
library(tidyr)
library(purrr)
nested_data %>%
   mutate(data = type.convert(data, as.is = TRUE)) %>% 
   unnest(data)

output

# A tibble: 150 × 6
   Species Sepal.Length Sepal.Width Petal.Length Petal.Width randomVar
   <fct>          <dbl>       <dbl>        <dbl>       <dbl>     <dbl>
 1 setosa           5.1         3.5          1.4         0.2        NA
 2 setosa           4.9         3            1.4         0.2        NA
 3 setosa           4.7         3.2          1.3         0.2        NA
 4 setosa           4.6         3.1          1.5         0.2        NA
 5 setosa           5           3.6          1.4         0.2        NA
 6 setosa           5.4         3.9          1.7         0.4        NA
 7 setosa           4.6         3.4          1.4         0.3        NA
 8 setosa           5           3.4          1.5         0.2        NA
 9 setosa           4.4         2.9          1.4         0.2        NA
10 setosa           4.9         3.1          1.5         0.1        NA
# … with 140 more rows

type.convert.convert将无法使用(由于字符元素,然后将列迫使列contracunnest,然后使用type.convert.convert更改列类型

nested_data %>%
  mutate(data = map(data,~ 
   .x %>% 
    mutate(across(everything(), as.character)))) %>% 
  unnest(data) %>% 
  type.convert(as.is = TRUE)

If the column types are different accidentally, then can use type.convert before the unnest

library(dplyr)
library(tidyr)
library(purrr)
nested_data %>%
   mutate(data = type.convert(data, as.is = TRUE)) %>% 
   unnest(data)

-output

# A tibble: 150 × 6
   Species Sepal.Length Sepal.Width Petal.Length Petal.Width randomVar
   <fct>          <dbl>       <dbl>        <dbl>       <dbl>     <dbl>
 1 setosa           5.1         3.5          1.4         0.2        NA
 2 setosa           4.9         3            1.4         0.2        NA
 3 setosa           4.7         3.2          1.3         0.2        NA
 4 setosa           4.6         3.1          1.5         0.2        NA
 5 setosa           5           3.6          1.4         0.2        NA
 6 setosa           5.4         3.9          1.7         0.4        NA
 7 setosa           4.6         3.4          1.4         0.3        NA
 8 setosa           5           3.4          1.5         0.2        NA
 9 setosa           4.4         2.9          1.4         0.2        NA
10 setosa           4.9         3.1          1.5         0.1        NA
# … with 140 more rows

Or if type.convert wouldn't work (because of character elements, then force the columns to be of type character, unnest and then change the column types with type.convert

nested_data %>%
  mutate(data = map(data,~ 
   .x %>% 
    mutate(across(everything(), as.character)))) %>% 
  unnest(data) %>% 
  type.convert(as.is = TRUE)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文