mutate

dplyr - mutate formula based on similarities in column names

半世苍凉 提交于 2019-11-29 12:42:34
I am trying to find a better way to run a mutate() on a combination of columns based on parts of the column names. For example, a way to simplify the mutate function in the following code: df <- data.frame(LIMITED_A = c(100,200), UNLIMITED_A = c(25000,50000), LIMITED_B = c(300,300), UNLIMITED_B = c(500,500), LIMITED_C = c(2,10), UNLIMITED_C = c(5,20)) df %>% mutate(FINAL_LIMITED = (LIMITED_A - LIMITED_B) / LIMITED_C, FINAL_UNLIMITED = (UNLIMITED_A - UNLIMITED_B) / UNLIMITED_C) A formula with the form: (._A - ._B) / ._C and the result is given the name FINAL_. Is there a way to simplify this to

How to use map from purrr with dplyr::mutate to create multiple new columns based on column pairs

孤者浪人 提交于 2019-11-28 07:02:15
I have to following issue using R. In short I want to create multiple new columns in a data frame based on calculations of different column pairs in the data frame. The data looks as follows: df <- data.frame(a1 = c(1:5), b1 = c(4:8), c1 = c(10:14), a2 = c(9:13), b2 = c(3:7), c2 = c(15:19)) df a1 b1 c1 a2 b2 c2 1 4 10 9 3 15 2 5 11 10 4 16 3 6 12 11 5 17 4 7 13 12 6 18 5 8 14 13 7 19 The output is supposed to look like the following: a1 b1 c1 a2 b2 c2 sum_a sum_b sum_c 1 4 10 9 3 15 10 7 25 2 5 11 10 4 16 12 9 27 4 7 13 12 6 18 16 13 31 5 8 14 13 7 19 18 15 33 I can achieve this using dplyr

dplyr - mutate formula based on similarities in column names

*爱你&永不变心* 提交于 2019-11-28 06:15:19
问题 I am trying to find a better way to run a mutate() on a combination of columns based on parts of the column names. For example, a way to simplify the mutate function in the following code: df <- data.frame(LIMITED_A = c(100,200), UNLIMITED_A = c(25000,50000), LIMITED_B = c(300,300), UNLIMITED_B = c(500,500), LIMITED_C = c(2,10), UNLIMITED_C = c(5,20)) df %>% mutate(FINAL_LIMITED = (LIMITED_A - LIMITED_B) / LIMITED_C, FINAL_UNLIMITED = (UNLIMITED_A - UNLIMITED_B) / UNLIMITED_C) A formula with

How to use map from purrr with dplyr::mutate to create multiple new columns based on column pairs

≡放荡痞女 提交于 2019-11-27 01:24:51
问题 I have to following issue using R. In short I want to create multiple new columns in a data frame based on calculations of different column pairs in the data frame. The data looks as follows: df <- data.frame(a1 = c(1:5), b1 = c(4:8), c1 = c(10:14), a2 = c(9:13), b2 = c(3:7), c2 = c(15:19)) df a1 b1 c1 a2 b2 c2 1 4 10 9 3 15 2 5 11 10 4 16 3 6 12 11 5 17 4 7 13 12 6 18 5 8 14 13 7 19 The output is supposed to look like the following: a1 b1 c1 a2 b2 c2 sum_a sum_b sum_c 1 4 10 9 3 15 10 7 25 2

dplyr mutate with conditional values

ⅰ亾dé卋堺 提交于 2019-11-26 23:49:52
In a large dataframe ("myfile") with four columns I have to add a fifth column with values conditionally based on the first four columns. Prefer answers with dplyr and mutate , mainly because of its speed in large datasets. My dataframe looks like this: V1 V2 V3 V4 1 1 2 3 5 2 2 4 4 1 3 1 4 1 1 4 4 5 1 3 5 5 5 5 4 ... The values of the fifth column (V5) are based on some conditional rules: if (V1==1 & V2!=4) { V5 <- 1 } else if (V2==4 & V3!=1) { V5 <- 2 } else { V5 <- 0 } Now I want to use the mutate function to use these rules on all rows (to avoid slow loops). Something like this (and yes, I

dplyr mutate with conditional values

主宰稳场 提交于 2019-11-26 08:47:56
问题 In a large dataframe (\"myfile\") with four columns I have to add a fifth column with values conditionally based on the first four columns. Prefer answers with dplyr and mutate , mainly because of its speed in large datasets. My dataframe looks like this: V1 V2 V3 V4 1 1 2 3 5 2 2 4 4 1 3 1 4 1 1 4 4 5 1 3 5 5 5 5 4 ... The values of the fifth column (V5) are based on some conditional rules: if (V1==1 & V2!=4) { V5 <- 1 } else if (V2==4 & V3!=1) { V5 <- 2 } else { V5 <- 0 } Now I want to use

Can dplyr package be used for conditional mutating?

穿精又带淫゛_ 提交于 2019-11-26 01:42:48
问题 Can the mutate be used when the mutation is conditional (depending on the values of certain column values)? This example helps showing what I mean. structure(list(a = c(1, 3, 4, 6, 3, 2, 5, 1), b = c(1, 3, 4, 2, 6, 7, 2, 6), c = c(6, 3, 6, 5, 3, 6, 5, 3), d = c(6, 2, 4, 5, 3, 7, 2, 6), e = c(1, 2, 4, 5, 6, 7, 6, 3), f = c(2, 3, 4, 2, 2, 7, 5, 2)), .Names = c(\"a\", \"b\", \"c\", \"d\", \"e\", \"f\"), row.names = c(NA, 8L), class = \"data.frame\") a b c d e f 1 1 1 6 6 1 2 2 3 3 3 2 2 3 3 4 4