R:错误:在dplyr中使用unnest时,长度不兼容 [英] R: Error: Incompatible lengths when using unnest in dplyr
问题描述
我有一个小标题(此处).我通过使用此原始数据集并运行以下数据(之前的文章
I have a large tibble (here). I created it by using this original dataset and running the following (previous post here):
#this code seemed to work
library(tidyverse)
df_tib <- df_full_subset %>%
pivot_longer(cols = everything(), names_to = c("name", ".value"), names_pattern = "(.*)_(.*)") %>%
select(-name) %>%
pivot_wider(names_from = "01", values_from = "02", values_fn = list)
从上一篇文章中可以看出,有最后一部分代码可以嵌套该数据.这对我不起作用,因此我修改了小标题,发现了几根垃圾柱(例如,一列NA),并删除了那些可能会有所帮助的想法.但是,我一直收到相同的错误:"错误:不兼容的长度:254、257"
.这对我来说就像 dplyr
在第254行和第257行中的NA挣扎一样,但是我看到其他帖子似乎很容易处理(例如
As can be seen in the previous post, there was a final bit of code to unnest that data. That didn't work for me so I tinkered with the tibble and found a few rubbish columns (e.g. a column of NAs), and removed those thinking that might help. However, I keep getting the same error: "Error: Incompatible lengths: 254, 257"
. This reads to me like dplyr
is struggling with NAs in rows 254 and 257, but I've seen other posts where this seems to be easily dealt with (like this one that used filter
), and those solutions did not work for this data.
#cleaning the data
df_tib$habitat <- df_tib$habitat_
df_tib$species <- df_tib$species_
df_tib <- janitor::clean_names(df_tib)
df_tib <- df_tib %>%
select(-habitat_,-species_, -na)
df_tib <- df_tib %>%
unnest(cols = everything()) #does not work
非常感谢您的帮助.
推荐答案
我刚刚注意到您的数据中有第一列 X
,不需要 包含在您的 pivot_longer
语句中.要透视除X以外的所有列,可以执行 pivot_longer(cols = -X,...)
.
I just noticed you have a first column X
in your data, that does not need to be included in your pivot_longer
statement. To pivot all columns except for X, you can do pivot_longer(cols = -X, ...)
.
我还添加了 drop_na
,您可能打算在 pivot_longer
之后添加.该 NA
列否则缺少值,objectid和editMode.
I also added drop_na
which you may have intended after pivot_longer
. This NA
column otherwise had missing values, objectid, and editMode.
尝试以下方法:
library(tidyverse)
df %>%
pivot_longer(cols = -X, names_to = c("name", ".value"), names_pattern = "(.*)_(.*)") %>%
drop_na %>%
select(-name) %>%
pivot_wider(names_from = "01", values_from = "02", values_fn = list) %>%
unnest(cols = everything())
输出
X bearing_degrees coordinates distance_meters habitat_ how_many_animal… observers species_ was_a_group_of_… was_the_animal_… width_of_the_ro… what_species_of… livestock
<int> <chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr>
1 1 200 N1.20701 E… 100 open_sc… 4 TW2 giraffe group_ "alive" single_lane NA NA
2 2 300 N1.20195 E… 20 commiph… NA TW2 o gerenuk individual "alive" single_lane NA NA
3 3 10 N1.18823 E… 80 open_sc… NA TW2 bird individual "alive" single_lane Ostrich NA
4 4 20 N1.15642 E… 180 open_sc… 300 TW2 livesto… group_ "alive" single_lane NA shoat
5 5 300 N1.14868 E… 30 open_sc… 7 TW2 livesto… group_ "alive" single_lane NA cattle
6 6 70 N1.13874 E… 200 open_sc… 34 TW2 livesto… group_ "alive" single_lane NA cattle
7 7 20 N1.11442 E… 40 disturb… 12 TW2 livesto… group_ "alive" single_lane NA cattle
8 8 NA N1.03003 E… NA commiph… NA TW2 jackal NA "roadkill" single_lane NA NA
9 9 40 N1.97961 E… 50 mixed_s… null TW2 gerenuk individual "alive,\n wh… NA NA null
10 10 20 N0.85822 E… 20 dense_s… 53 TW2 baboon group_ "alive" single_lane NA NA
# … with 251 more rows, and 5 more variables: approximate_age_of_individual <chr>, approximate_number_of_days_sinc <chr>, sex_of_animal <chr>, generated_note_survey <chr>,
# `_date` <chr>
这篇关于R:错误:在dplyr中使用unnest时,长度不兼容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!