mutate

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

这一生的挚爱 提交于 2020-07-22 21:33:18
问题 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

mutate df to generate several new columns using a function on several original fields

折月煮酒 提交于 2020-06-28 07:41:19
问题 I have the following dataframe in R showing several attributes for some community districts (field CD) in two different years: #Example data with one single attribute x <- structure(list(numbldgs = c(195, 845, 3621, 3214, 10738, 793, 223, 957, 4248, 3456, 11576, 803), Year = c("2007", "2007", "2007", "2007", "2007", "2007", "2018", "2018", "2018", "2018", "2018", "2018"), CD = c("103", "111", "210", "313", "414", "501", "103", "111", "210", "313", "414", "501")), row.names = c(NA, -12L),

Changing column types with dplyr

只愿长相守 提交于 2020-06-25 05:30:26
问题 I need some help tidying my data. I'm trying to convert some integers to factors (but not all integers to factors). I think I can do with selecting the variables in question but how do I add them back to the original data set? For example, keeping the values NOT selected from my raw_data_tbl and using the mutated types from the raw_data_tbl_int library(dplyr) raw_data_tbl %>% select_if(is.numeric) %>% select(-c(contains("units"), PRO_ALLOW, RTL_ACTUAL, REAL_PRICE, REAL_PRICE_HHU, REBATE,

How can you use a polynomial function programmatically in mutate?

笑着哭i 提交于 2020-06-16 04:17:48
问题 I want to use mutate to give me predicted values based on pre-specified variables with a quadratic / polynomial function. I can easily do this with a linear formula like this: library(tidyverse) xvar <- "Sepal.Length" yvar <- "Sepal.Width" ##linear fit #what does formula text look like? formula = !!sym(yvar) ~ !!sym(xvar) formula models <- iris %>% nest(-Species) %>% mutate( # Perform loess (or lm) calculation on each group m = map(data, lm, formula = !!sym(yvar) ~ !!sym(xvar) ), # Retrieve

Recode/replace variables conditionally with R dyplyr?

♀尐吖头ヾ 提交于 2020-06-09 04:06:32
问题 I am trying to use the following code to recode my variables conditionally so that the values in var 2 are replaced by NA if there is any value greater than 0 in var1. If var1 has no value greater than 0 (which is coded as NA), then the value in var2 should remain as it is. The line of code below codes everything in var2 to NA and does not keep the values in var2 if var1 is NA. I have also tried to use na_if() and coalesce() with not much success. Any possible fix to this? df <- df %>% mutate

Recode/replace variables conditionally with R dyplyr?

旧巷老猫 提交于 2020-06-09 04:06:24
问题 I am trying to use the following code to recode my variables conditionally so that the values in var 2 are replaced by NA if there is any value greater than 0 in var1. If var1 has no value greater than 0 (which is coded as NA), then the value in var2 should remain as it is. The line of code below codes everything in var2 to NA and does not keep the values in var2 if var1 is NA. I have also tried to use na_if() and coalesce() with not much success. Any possible fix to this? df <- df %>% mutate

How can I use mutate() and case_when() in a for loop?

别说谁变了你拦得住时间么 提交于 2020-05-27 21:18:47
问题 I'm writing a Shiny app where the user will be inputting data for conditions of their samples, and the script will "automatically" match their inputted conditions to sample names of a given file. For simplicity, I will not include the shiny code, because I am only struggling with the actual R implementation. If I already know what the potential conditions are, I could do something like: library(tidyverse) x <- data.frame(Samples = c('Low1', 'Low2', 'High1', 'High2', 'Ctrl1', 'Ctrl2')) x <- x

How to mutate a new column by modifying another column?

不问归期 提交于 2020-03-23 12:00:15
问题 I have very simple question. I have a df like, df1 <- data.frame(id=c(1,2,3)) I want to add a new column by adding 'z000' to existing id by using 'mutate' function of dplyr, My expectation is like, df2 <- data.frame(id=c(1,2,3),new_id=c("z0001","z0002","z0003")) Please let me know how to do. 回答1: You just need paste0 df1 %>% mutate(new_id = paste0("z000", id)) id new_id 1 1 z0001 2 2 z0002 3 3 z0003 来源: https://stackoverflow.com/questions/60638082/how-to-mutate-a-new-column-by-modifying

Is mutating object-parameters in a method(in Java) a bad practice?

随声附和 提交于 2020-03-22 03:44:48
问题 I have a question about mutating method-paramaters(which are objects) in a method. I read and heard multiple times that it is a bad practice to mutate a object in a method which was passed in as a paramater. As example: public void modifyList(List<Object> list) { list.add(new Object()); } Instead, the passed in Object should be copied, the mutation should be performed on the copied object and the copied object should be returned. As example: public List<Object> getModifiedList(List<Object>

R dplyr::mutate with ifelse conditioned on a global variable recycles result from first row

我的梦境 提交于 2020-02-25 06:10:20
问题 I am curious why an ifelse() statement within a call to dplyr::mutate() only seems to apply to the first row of my data frame. This returns a single value, which is recycled down the entire column. Since the expressions evaluated in either case of the ifelse() are only valid in the context of my data frame, I would expect the condition check and resulting expression evaluations to be performed on the columns as a whole, not just their first elements. Here's an example: I have a variable