R Megre数据帧列和重新编码
我有两个R数据帧,如下所示: 数据帧1: 标识符 后路 职位编号 分类 11111 0.260 1. 对 11111 0.0822 2. 对 11111 0.00797 3. 对 11111 0.04 4. 不 11111 0.245 5. 对 11111 0.432 6. 对 11112 0.342 1. 大概 11112 0.453 2. 对 11112 0.0032 3. 对 11112 0.241 5. 不 11112 0.0422 6. 对 11112 0.311 4. 不R Megre数据帧列和重新编码,r,dataframe,merge,R,Dataframe,Merge,我有两个R数据帧,如下所示: 数据帧1: 标识符 后路 职位编号 分类 11111 0.260 1. 对 11111 0.0822 2. 对 11111 0.00797 3. 对 11111 0.04 4. 不 11111 0.245 5. 对 11111 0.432 6. 对 11112 0.342 1. 大概 11112 0.453 2. 对 11112 0.0032 3. 对 11112 0.241 5. 不 11112 0.0422 6. 对 11112 0.311 4. 不 使用dpl
使用
dplyr
和case\u,在以下情况下,您可以非常轻松地重新编码:
df1 %>% mutate(
classification =
case_when( classification == "yes" ~ 1,
classification == "no" ~ 0,
classification == "maybe" ~ 2)
)
我将用以下方法解决它:
library(tidyverse)
df1 <- data.frame(
stringsAsFactors = FALSE,
identifier = c(11111L,11111L,11111L,11111L,
11111L,11111L,11112L,11112L,11112L,11112L,11112L,
11112L),
ef_posterior = c(0.26,0.0822,0.00797,0.04,
0.245,0.432,0.342,0.453,0.0032,0.241,0.0422,0.311),
position_no = c(1L, 2L, 3L, 4L, 5L, 6L, 1L, 2L, 3L, 5L, 6L, 4L),
classification = c("yes","yes","yes","no",
"yes","yes","maybe","yes","yes","no","yes","no")
)
df2 <- data.frame(
check.names = FALSE,
study_identifier = c(11111L, 11112L),
`%LVEF` = c(62L, 76L)
)
df1 %>% mutate(
classification =
case_when( classification == "yes" ~ 1,
classification == "no" ~ 0,
classification == "maybe" ~ 2)
) %>%
pivot_wider(
id_cols = c(identifier), names_from = c(position_no), values_from = c(classification,ef_posterior)) %>%
left_join(df2, by = c("identifier" = "study_identifier"))
#> # A tibble: 2 x 14
#> identifier classification_1 classification_2 classification_3 classification_4
#> <int> <dbl> <dbl> <dbl> <dbl>
#> 1 11111 1 1 1 0
#> 2 11112 2 1 1 0
#> # … with 9 more variables: classification_5 <dbl>, classification_6 <dbl>,
#> # ef_posterior_1 <dbl>, ef_posterior_2 <dbl>, ef_posterior_3 <dbl>,
#> # ef_posterior_4 <dbl>, ef_posterior_5 <dbl>, ef_posterior_6 <dbl>,
#> # `%LVEF` <int>
库(tidyverse)
df1%
支点更宽(
id\u cols=c(标识符),名称\u from=c(位置\u编号),值\u from=c(分类,ef\u后))%>%
左联接(df2,by=c(“标识符”=“研究标识符”))
#>#A tible:2 x 14
#>标识符分类\u 1分类\u 2分类\u 3分类\u 4
#>
#> 1 11111 1 1 1 0
#> 2 11112 2 1 1 0
#>#…还有9个变量:分类_5,分类_6,
#>#ef#U后#U 1,ef#U后#U 2,ef#U后#U 3,
#>#ef#U后4、ef#U后5、ef#U后6、,
#>#`%LVEF`
由reprex软件包(v0.3.0)于2021年4月12日创建