pivot_wider: 如何为非唯一值添加行?
pivot_wider: How to add rows for non unique values?
我有一个 table 长格式,带有不同语言的物种名称和 2 个级别的 ID 编号,一个对应于物种,另一个对应于该物种的不同名称。
例如:
species_list <- cbind.data.frame(num_sp= c(100,101,101,101,101,102,102,103), num_name = c(1,1,2,3,4,1,2,1), language=c("latin","latin", "english", "english", "english","latin","english","english"), name=c("marinus thingus", "aquaticus stuffae", "blob","water being","marine creature","altermarinus stuffae","other marine stuff", "unknown thingy"))
num_sp num_name language name
1 100 1 latin marinus thingus
2 101 1 latin aquaticus stuffae
3 101 2 english blob
4 101 3 english water being
5 101 4 english marine creature
6 102 1 latin altermarinus stuffae
7 102 2 english other marine stuff
8 103 1 english unknown thingy
我想要一个对应关系table 以便能够从英文名字中得到拉丁名字。我认为 table 应该在列中给我语言,在行中只给我 num_sp id,这对应于 pivot_wider
,除了我的“num_sp”不是唯一标识符,因此该函数会发出警告:
Warning message:
Values are not uniquely identified; output will contain list-cols.
有没有办法(通过 values_fn,也许?)将这些列表列拆分成不同的行,从而得到一个像这样的 table:
num_sp english latin
1 100 <NA> marinus thingus
2 101 blob aquaticus stuffae
3 101 water being aquaticus stuffae
4 101 marine creature aquaticus stuffae
5 102 other marine stuff altermarinus stuffae
6 103 unknown thingy <NA>
感谢您的宝贵时间!
这将解决问题
species_list %>% pivot_wider(id_cols = num_sp,
names_from = language,
values_from = name,
values_fn = list) %>%
unnest(-num_sp)
# A tibble: 6 x 3
num_sp latin english
<dbl> <chr> <chr>
1 100 marinus thingus NA
2 101 aquaticus stuffae blob
3 101 aquaticus stuffae water being
4 101 aquaticus stuffae marine creature
5 102 altermarinus stuffae other marine stuff
6 103 NA unknown thingy
您可以对 num_name
列重新编号,将数据转换为宽,fill
值。
library(dplyr)
library(tidyr)
species_list %>%
group_by(num_sp, language) %>%
mutate(num_name = row_number()) %>%
pivot_wider(names_from = language, values_from = name) %>%
fill(latin, english) %>%
ungroup
# num_sp num_name latin english
# <dbl> <int> <chr> <chr>
#1 100 1 marinus thingus NA
#2 101 1 aquaticus stuffae blob
#3 101 2 aquaticus stuffae water being
#4 101 3 aquaticus stuffae marine creature
#5 102 1 altermarinus stuffae other marine stuff
#6 103 1 NA unknown thingy
我有一个 table 长格式,带有不同语言的物种名称和 2 个级别的 ID 编号,一个对应于物种,另一个对应于该物种的不同名称。
例如:
species_list <- cbind.data.frame(num_sp= c(100,101,101,101,101,102,102,103), num_name = c(1,1,2,3,4,1,2,1), language=c("latin","latin", "english", "english", "english","latin","english","english"), name=c("marinus thingus", "aquaticus stuffae", "blob","water being","marine creature","altermarinus stuffae","other marine stuff", "unknown thingy"))
num_sp num_name language name
1 100 1 latin marinus thingus
2 101 1 latin aquaticus stuffae
3 101 2 english blob
4 101 3 english water being
5 101 4 english marine creature
6 102 1 latin altermarinus stuffae
7 102 2 english other marine stuff
8 103 1 english unknown thingy
我想要一个对应关系table 以便能够从英文名字中得到拉丁名字。我认为 table 应该在列中给我语言,在行中只给我 num_sp id,这对应于 pivot_wider
,除了我的“num_sp”不是唯一标识符,因此该函数会发出警告:
Warning message:
Values are not uniquely identified; output will contain list-cols.
有没有办法(通过 values_fn,也许?)将这些列表列拆分成不同的行,从而得到一个像这样的 table:
num_sp english latin
1 100 <NA> marinus thingus
2 101 blob aquaticus stuffae
3 101 water being aquaticus stuffae
4 101 marine creature aquaticus stuffae
5 102 other marine stuff altermarinus stuffae
6 103 unknown thingy <NA>
感谢您的宝贵时间!
这将解决问题
species_list %>% pivot_wider(id_cols = num_sp,
names_from = language,
values_from = name,
values_fn = list) %>%
unnest(-num_sp)
# A tibble: 6 x 3
num_sp latin english
<dbl> <chr> <chr>
1 100 marinus thingus NA
2 101 aquaticus stuffae blob
3 101 aquaticus stuffae water being
4 101 aquaticus stuffae marine creature
5 102 altermarinus stuffae other marine stuff
6 103 NA unknown thingy
您可以对 num_name
列重新编号,将数据转换为宽,fill
值。
library(dplyr)
library(tidyr)
species_list %>%
group_by(num_sp, language) %>%
mutate(num_name = row_number()) %>%
pivot_wider(names_from = language, values_from = name) %>%
fill(latin, english) %>%
ungroup
# num_sp num_name latin english
# <dbl> <int> <chr> <chr>
#1 100 1 marinus thingus NA
#2 101 1 aquaticus stuffae blob
#3 101 2 aquaticus stuffae water being
#4 101 3 aquaticus stuffae marine creature
#5 102 1 altermarinus stuffae other marine stuff
#6 103 1 NA unknown thingy