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") #>...
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...
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" ;...
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...
try df<-data.frame(VAR1,N, stringsAsFactors=FALSE)
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 #...
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 ...
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...
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...
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....
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...
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 ...