[R] Replace column values in R conditional on values from different column
arun
smartpink111 at yahoo.com
Sat Oct 13 21:31:13 CEST 2012
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.
More information about the R-help
mailing list