将 Dataframe 从长循环到宽以删除重复数据

jaxagkaj  于 2023-02-26  发布在  其他
关注(0)|答案(1)|浏览(148)

我有一个 Dataframe 有重复的person_id值(对于他们居住过的每个城市)。我想将 Dataframe 透视为宽格式以删除这些重复值。
原始 Dataframe :

> dput(df)
structure(list(Person.Id = c(123L, 345L, 345L, NA), City_lived = c("NY", 
"NY", "Boston"), Current_Patient = c("Yes", "Yes", "Yes"), DOB = c("11/20/97", "10/10/92", "10/10/92")), class = "data.frame", row.names = c(NA, 
-4L))

所需 Dataframe :

> dput(df2)
structure(list(Person.Id = c(123L, 345L), City_lived_Boston = c(0L, 
1L), City_lived_NY = c(1L, 1L), Current_Patient = c("Yes", 
"Yes"), DOB = c("11/20/97", "10/10/92")), class = "data.frame", row.names = c(NA, -5L))
vq8itlhq

vq8itlhq1#

试试看

library(dplyr)
library(tidyr)
df %>% 
  na.omit %>% 
  pivot_wider(names_from = City_lived, values_from = City_lived, 
   values_fn = length, values_fill = 0, names_prefix = 'City_lived_')

相关问题