GradStudentDD
2012-Oct-13 18:59 UTC
[R] Replace column values in R conditional on values from different column
Dear List, I am working on a stats project and have been stumped by the issue of replacing values in a column conditional on values from a different column. I searched the forum and google in general, and was able to put some code together, but it's not working the way it's supposed to... I appreciate any help you can offer! A detailed description of the issue, and the code I came up with, is below. I am trying to separate one variable (CohenSum) into separate values for males and females, in two separate columns, so I can do computations on them (i.e. mean, sd). I tried the code below, but the values I got didn't make sense - the length of the M and F columns should have been about half of the initial column length and NA for the rest (I have about half men and half women in the sample) but instead every cell has values in it, except for the initial NAs. I think R somehow inserted 0 instead of NAs for the gender values which were not supposed to be in that column (i.e. replaced values for females with 0 in the male column) but I don't know why. Thanks!!!!! for(i in 1:length(Health2$CohenSum)){ if (Health2$q_5a[i]==1) Health2$CohenM[i] <- Health2$CohenSum[i] else if (Health2$q_5a[i]==2) Health2$CohenF[i] <- Health2$CohenSum[i] else Health2$CohenF[i] <- NA } -- View this message in context: http://r.789695.n4.nabble.com/Replace-column-values-in-R-conditional-on-values-from-different-column-tp4646087.html Sent from the R help mailing list archive at Nabble.com.
Pascal Oettli
2012-Oct-13 19:15 UTC
[R] Replace column values in R conditional on values from different column
Hello, No need for a loop. "else if" doesn't exist in R. And Health2$CohenM and Health2_CohenF have to be created first, in case of a loop If Health2$q_5a only have 1 and 2 as possible values, just use "which" and "-". And I would suggest to not add new variables in your data frame, because you will include some NA. Create 2 new variables, then do your different calculations on. CohenM <- Health2$CohenSum[w] CohenF <- Health2$CohenSum[-w] Regards Pascal Le 12/10/13 20:59, GradStudentDD a ?crit :> Dear List, > > I am working on a stats project and have been stumped by the issue of > replacing values in a column conditional on values from a different column. > I searched the forum and google in general, and was able to put some code > together, but it's not working the way it's supposed to... I appreciate any > help you can offer! A detailed description of the issue, and the code I came > up with, is below. > > I am trying to separate one variable (CohenSum) into separate values for > males and females, in two separate columns, so I can do computations on them > (i.e. mean, sd). I tried the code below, but the values I got didn't make > sense - the length of the M and F columns should have been about half of the > initial column length and NA for the rest (I have about half men and half > women in the sample) but instead every cell has values in it, except for the > initial NAs. I think R somehow inserted 0 instead of NAs for the gender > values which were not supposed to be in that column (i.e. replaced values > for females with 0 in the male column) but I don't know why. > > Thanks!!!!! > > for(i in 1:length(Health2$CohenSum)){ > if (Health2$q_5a[i]==1) Health2$CohenM[i] <- Health2$CohenSum[i] > else if (Health2$q_5a[i]==2) Health2$CohenF[i] <- Health2$CohenSum[i] > else Health2$CohenF[i] <- NA > } > > > > -- > View this message in context: http://r.789695.n4.nabble.com/Replace-column-values-in-R-conditional-on-values-from-different-column-tp4646087.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help at 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. >
arun
2012-Oct-13 19:31 UTC
[R] Replace column values in R conditional on values from different column
HI, Not sure how your dataset looks like and not very clear whether this is what you want. Try this: set.seed(1) Health1<-data.frame(q_5=sample(1:2,10,replace=TRUE),CohenSum=rnorm(10,25)) Health2<-data.frame(Health1,CohenM=NA,CohenF=NA) idxM<-which(Health2$q_5==1) ?idxF<-which(Health2$q_5==2) Health2$CohenM[idxM]<-Health2$CohenSum[Health2$q_5==1] ?Health2$CohenF[idxF]<-Health2$CohenSum[Health2$q_5==2] ?Health2 #?? q_5 CohenSum?? CohenM?? CohenF #1??? 1 24.17953 24.17953?????? NA #2??? 1 25.48743 25.48743?????? NA #3??? 2 25.73832?????? NA 25.73832 #4??? 2 25.57578?????? NA 25.57578 #5??? 1 24.69461 24.69461?????? NA #6??? 2 26.51178?????? NA 26.51178 #7??? 2 25.38984?????? NA 25.38984 #8??? 2 24.37876?????? NA 24.37876 #9??? 2 22.78530?????? NA 22.78530 #10?? 1 26.12493 26.12493?????? NA A.K. ----- Original Message ----- From: GradStudentDD <dd7kc at virginia.edu> To: r-help at r-project.org Cc: Sent: Saturday, October 13, 2012 2:59 PM Subject: [R] Replace column values in R conditional on values from different column Dear List, I am working on a stats project and have been stumped by the issue of replacing values in a column conditional on values from a different column. I searched the forum and google in general, and was able to put some code together, but it's not working the way it's supposed to... I appreciate any help you can offer! A detailed description of the issue, and the code I came up with, is below. I am trying to separate one variable (CohenSum) into separate values for males and females, in two separate columns, so I can do computations on them (i.e. mean, sd). I tried the code below, but the values I got didn't make sense - the length of the M and F columns should have been about half of the initial column length and NA for the rest (I have about half men and half women in the sample) but instead every cell has values in it, except for the initial NAs. I think R somehow inserted 0 instead of NAs for the gender values which were not supposed to be in that column (i.e. replaced values for females with 0 in the male column) but I don't know why. Thanks!!!!! for(i in 1:length(Health2$CohenSum)){ ? if (Health2$q_5a[i]==1) Health2$CohenM[i] <- Health2$CohenSum[i] ? else if (Health2$q_5a[i]==2) Health2$CohenF[i] <- Health2$CohenSum[i] ? else Health2$CohenF[i] <- NA ? } -- View this message in context: http://r.789695.n4.nabble.com/Replace-column-values-in-R-conditional-on-values-from-different-column-tp4646087.html Sent from the R help mailing list archive at Nabble.com. ______________________________________________ R-help at 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.
GradStudentDD
2012-Oct-13 19:35 UTC
[R] Replace column values in R conditional on values from different column
Thank you so much!!! This is exactly what I needed!! Have a wonderful rest of the weekend!! -- View this message in context: http://r.789695.n4.nabble.com/Replace-column-values-in-R-conditional-on-values-from-different-column-tp4646087p4646091.html Sent from the R help mailing list archive at Nabble.com.