Re: [R] Recode Variable
> myData[myData$var1==5;"var1"]<-NA # recode value "5" into "NA" try myData[!is.na(myData$var1) & myData$var1==5;"var1"]<-NA or, more simply, myData$var1[myData$var1==5]<-NA *** This email and any attachments are confidential. Any use...{{dropped:8}} __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Recode Variable
1. Some data structured the way you are using would have been helpful. I used Tal Galil's play data and set up a dataframe with the variable names you are using: structure(list(var1 = c(1, NA, NA, 4, 5, 6, 7, 8, 9, 10, 5), var2 = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 5)), .Names = c("var1", "var2"), row.names = c(NA, -11L), class = "data.frame") > myData var1 var2 1 11 2NA2 3NA3 4 44 5 55 6 66 7 77 8 88 9 99 10 10 10 1155 2. The error message I get from your line of code is > myData[myData$var1==5;"var1"]<-NA Error: unexpected ';' in "myData[myData$var1==5;" Probably the semikolon is a typo? 3. If I understand your question correctly, the easiest answer I can find is ifelse(): > myData$var1 <- ifelse( myData$var1 == 5, NA, myData$var1 ) > myData var1 var2 1 11 2NA2 3NA3 4 44 5NA5 6 66 7 77 8 88 9 99 10 10 10 11 NA5 Rgds, Rainer On Thursday 12 April 2012 11:08:45 David Studer wrote: > Hello everybody, > > I know this is pretty basic stuff, but could anyone explain me how to > recode a single value of a variable > into a missing value? > > I used to do it like this: > > myData[myData$var1==5;"var1"]<-NA # recode value "5" into "NA" > > But the column "var1" already contains NAs, which > results in the following error message: > > "missing values are not allowed in subscripted assignments of data frames" > > Thank you very much for any advice! > > David > > [[alternative HTML version deleted]] > > __ > R-help@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Recode Variable
Le jeudi 12 avril 2012 à 11:08 +0200, David Studer a écrit : > Hello everybody, > > I know this is pretty basic stuff, but could anyone explain me how to > recode a single value of a variable > into a missing value? > > I used to do it like this: > > myData[myData$var1==5;"var1"]<-NA # recode value "5" into "NA" > > But the column "var1" already contains NAs, which > results in the following error message: > > "missing values are not allowed in subscripted assignments of data frames" > > Thank you very much for any advice! You can just do this: myData <- data.frame(var1=1:10) myData$var1[2]<-NA myData[myData$var1 == 5, "var1"] <- NA # Fails myData$var1[myData$var1 == 5] <- NA # Works myData var1 1 1 2NA 3 3 4 4 5NA 6 6 7 7 8 8 9 9 10 10 Regards __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Recode Variable
Le jeudi 12 avril 2012 à 12:29 +0300, Tal Galili a écrit : > Hi David, > You bring up a good question. I am not sure what is the "right" way to > solve it. But here is a simple solution I put together: > > x = c(1:10,5) > y = x > x[c(2,3)] <- NA > > # reproducing the problem: > y[x==5] > > na2F <- function(x) { > x2 <- x > x2[is.na(x)] <- F > x2 > } > na2F(x==5) > > # "solved" > y[na2F(x==5)] > > > I'd be happy to see other solutions to it. You can simply use the built-in function which() for this, since it removes NAs, only returning the position of TRUE elements: which(c(NA, 1:10 == 5)) [1] 6 My two cents __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Recode Variable
Hi David, You bring up a good question. I am not sure what is the "right" way to solve it. But here is a simple solution I put together: x = c(1:10,5) y = x x[c(2,3)] <- NA # reproducing the problem: y[x==5] na2F <- function(x) { x2 <- x x2[is.na(x)] <- F x2 } na2F(x==5) # "solved" y[na2F(x==5)] I'd be happy to see other solutions to it. With regards, Tal Contact Details:--- Contact me: tal.gal...@gmail.com | 972-52-7275845 Read me: www.talgalili.com (Hebrew) | www.biostatistics.co.il (Hebrew) | www.r-statistics.com (English) -- On Thu, Apr 12, 2012 at 12:08 PM, David Studer wrote: > Hello everybody, > > I know this is pretty basic stuff, but could anyone explain me how to > recode a single value of a variable > into a missing value? > > I used to do it like this: > > myData[myData$var1==5;"var1"]<-NA # recode value "5" into "NA" > > But the column "var1" already contains NAs, which > results in the following error message: > > "missing values are not allowed in subscripted assignments of data frames" > > Thank you very much for any advice! > > David > >[[alternative HTML version deleted]] > > __ > R-help@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. > [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] Recode Variable
Hello everybody, I know this is pretty basic stuff, but could anyone explain me how to recode a single value of a variable into a missing value? I used to do it like this: myData[myData$var1==5;"var1"]<-NA # recode value "5" into "NA" But the column "var1" already contains NAs, which results in the following error message: "missing values are not allowed in subscripted assignments of data frames" Thank you very much for any advice! David [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] recode Variable in dependence of values of two other variables
Hi: Here are several equivalent ways to produce your desired output: # Base package: transform() df <- transform(df, mean = ave(x, id, FUN = mean)) # plyr package library('plyr') ddply(df, .(id), transform, mean = mean(x)) # data.table package library('data.table') dt <- data.table(df, key = 'id') dt[, list(x, mean = mean(x)), by = 'id'] # doBy package library('doBy') transformBy(~ id, data = df, mean = mean(x)) HTH, Dennis On Fri, Aug 12, 2011 at 8:10 AM, Julia Moeller wrote: > Hi, > > as an R-beginner, I have a recoding problem and hope you can help me: > > I am working on a SPSS dataset, which I loaded into R (load("C:/...) > > I have 2 existing Variables: "ID" and "X" , > and one variable to be computed: meanX.dependID (=mean of X for all rows in > which ID has the same value) > > ID = subject ID. Since it is a longitudinal dataset, there are repeated > measurement points for each subject, each of which appears in a new row. So, > each ID value appears in many rows. (e.g. ID ==1 in row 1:5; ID ==2 in rows > 6:8 etc). > > > Now: For all rows, in which ID has a certain value, meanX.dependID shall be > the mean of X in for these rows. How can I automatisize that, without having > to specify the number of the rows each time? > > e.g. > > > ID X meanX.dependID > 1 2 2.25 > 1 3 2.25 > 1 1 2.25 > 1 3 2.25 > 2 5 3.3 > 2 2 3.3 > 2 3 3.3 > 3 4 3 > 3 1 3 > 3 2 3 > 3 3 3 > 3 4 3 > 3 5 3 > > > Thanks a lot! Hope this is the right place to post, if not, please tell me! > best, > Julia > > __ > R-help@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. > > __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] recode Variable in dependence of values of two other variables
?aggregate aggregate(X~ID, your.data.frame.goes.here, "mean") Mikhail > -Original Message- > From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On > Behalf Of Julia Moeller > Sent: Friday, August 12, 2011 10:10 AM > To: r-help@r-project.org > Subject: [R] recode Variable in dependence of values of two other variables > > Hi, > > as an R-beginner, I have a recoding problem and hope you can help me: > > I am working on a SPSS dataset, which I loaded into R (load("C:/...) > > I have 2 existing Variables: "ID" and "X" , and one variable to be > computed: meanX.dependID (=mean of X for all rows in which ID has the same > value) > > ID = subject ID. Since it is a longitudinal dataset, there are repeated > measurement points for each subject, each of which appears in a new row. > So, each ID value appears in many rows. (e.g. ID ==1 in row 1:5; ID ==2 in > rows 6:8 etc). > > > Now: For all rows, in which ID has a certain value, meanX.dependID shall be > the mean of X in for these rows. How can I automatisize that, without > having to specify the number of the rows each time? > > e.g. > > > IDXmeanX.dependID > 122.25 > 132.25 > 112.25 > 132.25 > 253.3 > 223.3 > 233.3 > 343 > 313 > 323 > 333 > 343 > 353 > > > Thanks a lot! Hope this is the right place to post, if not, please tell me! > best, > Julia __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] recode Variable in dependence of values of two other variables
Hi, as an R-beginner, I have a recoding problem and hope you can help me: I am working on a SPSS dataset, which I loaded into R (load("C:/...) I have 2 existing Variables: "ID" and "X" , and one variable to be computed: meanX.dependID (=mean of X for all rows in which ID has the same value) ID = subject ID. Since it is a longitudinal dataset, there are repeated measurement points for each subject, each of which appears in a new row. So, each ID value appears in many rows. (e.g. ID ==1 in row 1:5; ID ==2 in rows 6:8 etc). Now: For all rows, in which ID has a certain value, meanX.dependID shall be the mean of X in for these rows. How can I automatisize that, without having to specify the number of the rows each time? e.g. IDXmeanX.dependID 122.25 132.25 112.25 132.25 253.3 223.3 233.3 343 313 323 333 343 353 Thanks a lot! Hope this is the right place to post, if not, please tell me! best, Julia __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.