繁体   English   中英

使用dplyr根据另一列中的值添加新列

[英]adding a new column based upon values in another column using dplyr

我有一列数据框df$c_touch

c_touch
0
1
3
2
3
4
5

其中每个数字表示持续时间,例如0 = 2 mins, 1 = 5 mins, 2 = 10 mins, 3=15 mins, 4=20 mins, 5=30 mins

我想添加另一列df$c_duration就像

c_touch c_duration
0 2
1 5
3 15
2 10
3 15
4 20
5 30

到目前为止,我一直在使用一个循环,这有点丑陋/混乱,我宁愿不使用它。 是否存在添加额外列的无循环方法,尤其是使用dplyr mutate函数(因为我正尝试使用dplyr重写所有代码)?

这是dplyr解决方案:

# data.frame containing the mapping
map <- data.frame(
    idx = 0:5,
    val = c(2, 5, 10, 15, 20, 30))

# Sample data   
df <- read.table(text =
    "c_touch
0
1
3
2
3
4
5", header = T)

dplyr::left_join(df, map, by = c("c_touch" = "idx"))
#  c_touch val
#1       0   2
#2       1   5
#3       3  15
#4       2  10
#5       3  15
#6       4  20
#7       5  30
df %>%
 mutate(c_duration = case_when(
 c_touch == 0 ~ 2,
 c_touch == 5 ~ 30,
 T ~ c_touch * 5))

您可以在mutate内部使用dplyr :: case_wh:

df <- df %>%
    mutate(c_duration = case_when(c_touch == 0 ~ 2,
        c_touch == 1 ~ 5,
        c_touch == 2 ~ 10,
        c_touch == 3 ~ 15,
        c_touch == 4 ~ 20,
        c_touch == 5 ~ 30))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM