Menu
  • HOME
  • TAGS

how to convert a factor variable into a numeric - using R

r,variables,numeric,data-type-conversion,recode

First, here's why you would have to convert to character before converting to numeric: Lets say we have a factor that contains a handful of numbers x = factor(c(1,2,7,7)) you can inspect how this is represented in R like so: unclass(x) #> [1] 1 2 3 3 #> attr(,"levels") #>...

Conditionally Revalue a Factor in R

r,recode

There is no "6" level in gears to begin with, so you need to create one: levels(mtcars$gear) <- c(levels(mtcars$gear), "6") You can then conditionally assign with the [<- function: mtcars$gear[ mtcars$am==1 ] <- "6" table(mtcars$gear, mtcars$am) 0 1 3 15 0 4 4 0 5 0 0 6 0 13...

Recode in Car package returns unexpected symbol when recoding strings

r,car,recode

I was the author of the wordpress document - code is wrong and thanks for flagging the issue. Problem is that car::recode syntax requires a single quote rather than a double quote (or see comment from @MrFlick below on other acceptable syntax). y1 <- recode(y, 'c("Perch", "Goby") = "Perciform" ;...

R - converting character variable as.numeric, but in descending alphabetical order

r,recode

You should be able to just re-factor, based on rev(levels(Mark)): set.seed(10) x <- factor(sample(LETTERS[1:5], 10, TRUE)) x # [1] C B C D A B B B D C # Levels: A B C D as.numeric(x) # [1] 3 2 3 4 1 2 2 2 4 3 as.numeric(factor(x, levels...

r recode a string variable based on a condition

r,recode

try df<-data.frame(VAR1,N, stringsAsFactors=FALSE)

Change the coding of two variables in two data.tables and then merge data.table

r,merge,data.table,recode

From what you describe, you might be looking for rbind, not merge. Try the following: rbind(odd[, time_x := time * 2 - 1], even[, time_x := time * 2])[, time := NULL][] # id emotion learning time_x # 1: 11 4 60 1 # 2: 11 6 50 3 #...

Recode values omitting NA's

r,missing-data,recode

You can do try[try <= .8] <- 5 The NA values will remain as NA Or create a logical condition to exclude the NA values try[try <=.8 & !is.na(try)] <- 5 ...

How to recode a set of variables in a dataframe in R

r,reverse,lapply,recode

There's no need for an external function or for a package for this. Just use an anonymous function in lapply, like this: df[recode.list] <- lapply(df[recode.list], function(x) 6-x) Using [] lets us replace just those columns directly in the original dataset. This is needed since just using lapply would result in...

Recursively recode all project files excluding some directories and preserving permissions

linux,shell,find,recode

Based on this question, but its solution does not preserve permissions, so I had to modify it. WARNING: since the recursive removal is a part of the solution, use it on your own risk Task: Recursively recode all project files (iso8859-8 -> utf-8) excluding '.git' and '.idea' dirs and preserving...

Recode the same value pattern for all variables in Stata

binary,stata,recode

Let's suppose that you are looking for variables with a certain value label attached. You can retrieve those variables using ds and pass their names to recode. . clear . set obs 2 obs was 0, now 2 . forval j = 1/5 { 2. gen y`j' = _n 3....

R: Recoding several characters into one new factor

r,dummy-data,recode

Assuming that you wanted to check whether a subset of "countrycodes" are there in each of the "country" variables with the condition that if atleast one of the "countrycode" is present in a particular row, that row will get "1", or else "0". The idea is to create a vector...

Recoding variables with NAs in R

r,na,recode

You have three different conditions, so it's most natural to express it in three lines: z <- rep(0,nrow(frame)) z[apply(is.na(frame),1,all)] <- NA z[apply(frame==1 ,1,any)] <- 1 # [1] NA 0 1 ...