[英]Dealing with NAs after running pivot_wider
I have a long dataframe that I want to make wide using pivot_wider
:我有一个很长的数据pivot_wider
,我想使用pivot_wider
使其pivot_wider
:
library(tidyr)
example_data <- data.frame(
name = c("bob", "bob", "dick", "dick", "harry", "harry"),
sport = c("baseball", "football", "hockey", "basketball", "football", "basketball")
)
pivot_wider(example_data, names_from = sport, values_from = sport)
This gives the expected result, but with lots of NA
s这给出了预期的结果,但有很多NA
s
name baseball football hockey basketball
1 bob baseball football NA NA
2 dick NA NA hockey basketball
3 harry NA football NA basketball
I want to convert the sport names to TRUE
(since the name of the sport is already indicated by the column name) and convert the NA
s to FALSE
, creating a dataframe like this:我想将运动名称转换为TRUE
(因为运动名称已经由列名指示)并将NA
转换为FALSE
,创建这样的数据框:
name baseball football hockey basketball
1 bob TRUE TRUE FALSE FALSE
2 dick FALSE FALSE TRUE TRUE
3 harry FALSE TRUE FALSE TRUE
I thought this code would do the trick, but it threw an error:我认为这段代码可以解决问题,但它抛出了一个错误:
pivot_wider(
example_data,
names_from = sport,
values_from = sport,
values_fill = list(sport = FALSE),
values_fn = list(sport = !is.na)
)
Error in !is.na : invalid argument type
The code below gets me the reverse of what I'm looking for, which I could then work to convert to the desired dataframe:下面的代码让我找到了我正在寻找的相反的东西,然后我可以将其转换为所需的数据帧:
pivot_wider(
example_data,
names_from = sport,
values_from = sport,
values_fill = list(sport = TRUE),
values_fn = list(sport = is.na)
)
Is there a way to get directly to the desired dataframe?有没有办法直接获得所需的数据帧? And are there any tutorials for how to use the values_fn
argument so I can figure out why values_fn = list(sport = !is.na)
isn't working?是否有关于如何使用values_fn
参数的教程,以便我弄清楚为什么values_fn = list(sport = !is.na)
不起作用? Thanks.谢谢。
One way would be to create a dummy column with TRUE
values and then use pivot_wider
.一种方法是创建一个具有TRUE
值的虚拟列,然后使用pivot_wider
。
library(dplyr)
library(tidyr)
example_data %>%
mutate(val = TRUE) %>%
pivot_wider(names_from = sport,values_from = val,values_fill = list(val = FALSE))
# A tibble: 3 x 5
# name baseball football hockey basketball
# <fct> <lgl> <lgl> <lgl> <lgl>
#1 bob TRUE TRUE FALSE FALSE
#2 dick FALSE FALSE TRUE TRUE
#3 harry FALSE TRUE FALSE TRUE
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.