recode

R: Recoding variables using recode, mutate and case_when

喜欢而已 提交于 2021-02-18 03:23:17
问题 I want to recode the following values < 4 = -1, 4 = 0, > 4 = 1 for the following variables defined by core.vars in the dataset, and still keep the rest of the variables in the data frame. temp.df <- as.tibble (mtcars) other.vars <- c('hp', 'drat', 'wt') core.vars <- c('mpg', 'cyl', 'disp') temp.df <- rownames_to_column (temp.df, var ="cars_id") temp.df <- temp.df %>% mutate_if (is.integer, as.numeric) I have tried a number of ways to implement this. Using case_when , mutate , recode but with

R: Recoding variables using recode, mutate and case_when

强颜欢笑 提交于 2021-02-18 03:21:40
问题 I want to recode the following values < 4 = -1, 4 = 0, > 4 = 1 for the following variables defined by core.vars in the dataset, and still keep the rest of the variables in the data frame. temp.df <- as.tibble (mtcars) other.vars <- c('hp', 'drat', 'wt') core.vars <- c('mpg', 'cyl', 'disp') temp.df <- rownames_to_column (temp.df, var ="cars_id") temp.df <- temp.df %>% mutate_if (is.integer, as.numeric) I have tried a number of ways to implement this. Using case_when , mutate , recode but with

How to recode dataframe values to keep only those that satisfy a certain set, replace others with “other”

做~自己de王妃 提交于 2021-01-29 01:35:02
问题 I'm looking for a concise solution, preferably using dplyr , to clean up values in a dataframe column so that I can keep as they are values that match a certain set, but others that don't match will be recoded as "other". Example I have a dataframe with names of animals. There are 4 legit animal names, but other rows contain gibberish rather than names. I want to clean the column up, to keep only the legit animal names: zebra , lion , cow , or cat . Data library(tidyverse) library(stringi)

Assign a factor vector with more than 2 levels/labels for a given numeric numeric vector [duplicate]

◇◆丶佛笑我妖孽 提交于 2020-06-23 12:30:45
问题 This question already has answers here : Convert continuous numeric values to discrete categories defined by intervals (2 answers) Closed 15 days ago . everybody. I hope that you would help me to solve my query. For a vector representing the price ($) of apples say, apple <- c(23, 26, 54, 34, 34, 34, 98, 23, 4, 34, 098, 45, 93, 20, 39, 83, 78, 34, 09, 8, 56, 98, 99, 62, 29) I can assign a factor vector that represents whether it is "cheap" if apples cost less than $50 and "expensive" if apple

Assign a factor vector with more than 2 levels/labels for a given numeric numeric vector [duplicate]

心已入冬 提交于 2020-06-23 12:30:07
问题 This question already has answers here : Convert continuous numeric values to discrete categories defined by intervals (2 answers) Closed 15 days ago . everybody. I hope that you would help me to solve my query. For a vector representing the price ($) of apples say, apple <- c(23, 26, 54, 34, 34, 34, 98, 23, 4, 34, 098, 45, 93, 20, 39, 83, 78, 34, 09, 8, 56, 98, 99, 62, 29) I can assign a factor vector that represents whether it is "cheap" if apples cost less than $50 and "expensive" if apple

Recode numeric values in R

a 夏天 提交于 2020-06-22 22:45:28
问题 I want to recode some numeric values into different numeric values and have had a go using the following code: survey$KY27PHYc <- revalue(survey$KY27PHY1, c(5=3, 4=2,3=2,2=1,1=1)) I get the following error: ## Error: unexpected '=' in "survey$KY27PHYc <- revalue(survey$KY27PHY1, c(5=" Where am I going wrong? 回答1: This function does not work on numeric vector. If you want to use it, you can do as follows: x <- 1:10 # your numeric vector as.numeric(revalue(as.character(x), c("2" = "33", "4" =

R: How to recode multiple variables at once

扶醉桌前 提交于 2020-01-31 03:54:06
问题 I have several variables in my dataset that need to be recoded in exactly the same way, and several other variables that need to be recoded in a different way. I tried writing a function to help me with this, but I'm having trouble. library(dplyr) recode_liberalSupport = function(arg1){ arg1 = recode(arg1, "1=-1;2=1;else=NA") return(arg1) } liberals = c(df$var1, df$var4, df$var8) for(i in unique(liberals)){ paste(df$liberals[i] <- sapply(liberals, FUN = recode_liberalSupport)) } R studio

Recode dates to study day within subject

非 Y 不嫁゛ 提交于 2020-01-30 06:55:27
问题 I have data in which subjects completed multiple ratings per day over 6-7 days. The number of ratings per day varies. The data set includes subject ID, date, and the ratings. I would like to create a new variable that recodes the dates for each subject into "study day" --- so 1 for first day of ratings, 2 for second day of ratings, etc. For example, I would like to take this: id Date Rating 1 10/20/2018 2 1 10/20/2018 3 1 10/20/2018 5 1 10/21/2018 1 1 10/21/2018 7 1 10/21/2018 9 1 10/22/2018