[英]adding a new column based upon values in another column using dplyr
I have a column of a data frame df$c_touch
: 我有一列数据框
df$c_touch
:
c_touch
0
1
3
2
3
4
5
Where each number refers to a duration of time, such that 0 = 2 mins, 1 = 5 mins, 2 = 10 mins, 3=15 mins, 4=20 mins, 5=30 mins
. 其中每个数字表示持续时间,例如
0 = 2 mins, 1 = 5 mins, 2 = 10 mins, 3=15 mins, 4=20 mins, 5=30 mins
。
I'd like to add another column df$c_duration
to be like 我想添加另一列
df$c_duration
就像
c_touch c_duration
0 2
1 5
3 15
2 10
3 15
4 20
5 30
So far I've been using a loop, which is a bit ugly/messy, and I'd rather not use it. 到目前为止,我一直在使用一个循环,这有点丑陋/混乱,我宁愿不使用它。 Is there a loop-free way of adding the extra column in, particularly using dplyr mutate function (as I'm trying to rewrite all my code using dplyr)?
是否存在添加额外列的无循环方法,尤其是使用dplyr mutate函数(因为我正尝试使用dplyr重写所有代码)?
Here is a dplyr
solution: 这是
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))
You could use dplyr::case_when inside mutate: 您可以在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.