mutate

Date columns with NAs in R - unexpected behaviour with mutate

醉酒当歌 提交于 2020-08-27 19:55:04
问题 I'm trying to follow this process with a dataset. Here is a test dataframe: id <- c("Johnboy","Johnboy","Johnboy") orderno <- c(2,2,1) validorder <- c(0,1,1) ordertype <- c(95,94,95) orderdate <- as.Date(c("2019-06-17","2019-03-26","2018-08-23")) df <- data.frame(id, orderno, validorder, ordertype, orderdate) Then I do the following: ## compute order date for order types df <- df %>% mutate(orderdate_dried = if_else(validorder == 1 & ordertype == 95, orderdate, as.Date(NA)), orderdate_fresh =

Date columns with NAs in R - unexpected behaviour with mutate

给你一囗甜甜゛ 提交于 2020-08-27 19:52:07
问题 I'm trying to follow this process with a dataset. Here is a test dataframe: id <- c("Johnboy","Johnboy","Johnboy") orderno <- c(2,2,1) validorder <- c(0,1,1) ordertype <- c(95,94,95) orderdate <- as.Date(c("2019-06-17","2019-03-26","2018-08-23")) df <- data.frame(id, orderno, validorder, ordertype, orderdate) Then I do the following: ## compute order date for order types df <- df %>% mutate(orderdate_dried = if_else(validorder == 1 & ordertype == 95, orderdate, as.Date(NA)), orderdate_fresh =

Refer to column names dynamically inside mutate statements - dplyr

◇◆丶佛笑我妖孽 提交于 2020-08-22 06:07:11
问题 I stat apologize for the long question, but after quite a while I couldn't figure out a solution myself. I have this toy dataframe set.seed(23) df <- tibble::tibble( id = paste0("00", 1:6), cond = c(1, 1, 2, 2, 3, 3), A_1 = sample(0:9, 6, replace = TRUE), A_2 = sample(0:9, 6, replace = TRUE), A_3 = sample(0:9, 6, replace = TRUE), B_1 = sample(0:9, 6, replace = TRUE), B_2 = sample(0:9, 6, replace = TRUE), B_3 = sample(0:9, 6, replace = TRUE), C_1 = sample(0:9, 6, replace = TRUE), C_2 = sample

Using dplyr to group_by and conditionally mutate only with if (without else) statement

青春壹個敷衍的年華 提交于 2020-08-04 16:50:06
问题 I have a dataframe that I need to group by a combination of columns entries in order to conditionally mutate several columns using only an if statement (without an else condition). More specifically, I want to sum up the column values of a certain group if they cross a pre-defined threshold, otherwise the values should remain unchanged. I have tried doing this using both if_else and case_when but these functions require either a "false" argument ( if_else ) or by default set values that are

Using dplyr to group_by and conditionally mutate only with if (without else) statement

廉价感情. 提交于 2020-08-04 16:48:53
问题 I have a dataframe that I need to group by a combination of columns entries in order to conditionally mutate several columns using only an if statement (without an else condition). More specifically, I want to sum up the column values of a certain group if they cross a pre-defined threshold, otherwise the values should remain unchanged. I have tried doing this using both if_else and case_when but these functions require either a "false" argument ( if_else ) or by default set values that are

If values in a range of columns aren't present in another column, replace with NA

元气小坏坏 提交于 2020-07-22 21:33:39
问题 I have a dataset that includes some non-referenced data that I would like to replace with NA. In the following example, if the data in columns rep1 to rep4 does not match one of the values in the ID column, I would like to replace the value with NA. In this case, the values of x, y, and z aren't listed in the ID column, so they should be replaced. This is a somewhat similar question that I asked earlier here : If data present, replace with data from another column based on row ID I think the