[R] Help with IF command strings

arun smartpink111 at yahoo.com
Fri Jul 12 14:21:59 CEST 2013


Hi,

Not sure I understand your question.
Suppose `data1` is your real data, but if the column names are different, change "V21", "V2" by those in the real data. Based on your initial post, the column names seemed to be the same.
mean(data1$V21[data1$V2==1|data1$V2==0])

A.K.  


What values would I substitute by real data.  I did everything the way 
you posted, and I got 3.8 as well.  So I'm curious what values I would 
change to get the mean for the actual data? 


----- Original Message -----
From: arun <smartpink111 at yahoo.com>
To: R help <r-help at r-project.org>
Cc: 
Sent: Thursday, July 11, 2013 9:21 PM
Subject: Re: Help with IF command strings

HI,
Try this:
set.seed(485)
dat1<- as.data.frame(matrix(sample(0:10,26*10,replace=TRUE),ncol=26))
mean(dat1$V21[dat1$V2==1|dat1$V2==0])
#[1] 3.8
#or
with(dat1,mean(V21[V2==1|V2==0]))
#[1] 3.8


A.K.


I have data in 26 columns, I'm trying to get a mean for column 21 only for the participants that are either 0 or 1 in column 2. 

One of the commands I tried looked something like this 

mean(data1$V21, if(V2 = 1))   

So basically I need to have the program run a mean (and later 
other forms of analysis) on participants based on their condition. 
either 0 or 1. 

Help is greatly appreciated. 

Thanks



More information about the R-help mailing list