简体   繁体   English

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

[英]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.

相关问题 使用 dplyr 根据来自另一列的值的总和创建一个新列 - Create a new column based on the sum of values from another column, with dplyr 如何使用dplyr根据另一列的不同值在新列中填充不同的值? - How to fill different values in a new column based on different values of another column using dplyr? 有没有办法在 R 中使用 dplyr 根据另一个列的值创建一个新列? - Is there a way to create a new column based on the values of another one using dplyr in R? 根据 R 中另一列的字符值添加新列 - adding a new column based on character values of another column in R R中的dplyr变异-根据另一列的顺序添加新列 - dplyr mutate in R - adding a new column depending on sequence of another column R - 基于使用另一列的函数为一列添加值 - R - adding values for one column based on a function using another column 根据该列中的滞后值改变新列 - dplyr 方法 - Mutate a new column based on lagged values within that column - dplyr approach 有没有办法使用dplyr基于另一列的group_by除以创建新列? - Is there a way using dplyr to create a new column based on dividing by group_by of another column? 如何使用dplyr根据另一列中的字符值的一部分更新列值? - how to renew column values based on part of character value in another column using dplyr? r、dplyr:如何使用 gsub 根据另一列中的值转换一列中的值 - r, dplyr: how to transform values in one column based on value in another column using gsub
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM