I have been wrestling with this function for quite a while, and am not making headway. 1) I want to apply a function to the following columns of a dataframe: myfunction. <- apply(ph5028[,c(83:107)],2,function(x) ... 2) Within each of the above columns there is a single numeric code, 1, 2 or 3 or an NA. 3) My goal is to determine the percent of time each person used a 2 code. So if a person across these columns had say 8 numerical entries and if 4 of these were the number 2, the answer for that person would be 50%. Best regards, Greg Blevins The Market Solutions Group [[alternative HTML version deleted]]
I think you need to give an example of input and output to clarify your question but in case this helps, if x is a numeric vector then mean(x==2, na.rm=T) gives the proportion of 2s among the non-NAs in x. Greg Blevins <gblevins <at> mn.rr.com> writes: : : I have been wrestling with this function for quite a while, and am not making headway. : : 1) I want to apply a function to the following columns of a dataframe: : : myfunction. <- apply(ph5028[,c(83:107)],2,function(x) ... : : 2) Within each of the above columns there is a single numeric code, 1, 2 or 3 or an NA. : : 3) My goal is to determine the percent of time each person used a 2 code. So if a person across these columns had : say 8 numerical entries and if 4 of these were the number 2, the answer for that person would be 50%. : : Best regards, : : Greg Blevins : The Market Solutions Group
Gabor,
Here is some further clarification using a toy example.
dataframe layout
v1 v2 v3 v4
Person1 1 NA 3 2
Person2 2 2 2 2
Person3 1 NA 2 2
I am looking for the proportion of time each person used a 2 code across the
variables v1 to v4. For example, using the above data, the answer would look
as follows:
Person1 33%
Person2 100%
Person3 66%
Thanks again,
Greg Blevins
----- Original Message -----
From: "Gabor Grothendieck" <ggrothendieck at myway.com>
To: <r-help at stat.math.ethz.ch>
Sent: Saturday, June 05, 2004 11:51 PM
Subject: Re: [R] Request help writing a function
>
> I think you need to give an example of input and output to clarify your
> question but in case this helps, if x is a numeric vector then
> mean(x==2, na.rm=T) gives the proportion of 2s among the non-NAs in x.
>
> Greg Blevins <gblevins <at> mn.rr.com> writes:
>
> :
> : I have been wrestling with this function for quite a while, and am not
> making headway.
> :
> : 1) I want to apply a function to the following columns of a dataframe:
> :
> : myfunction. <- apply(ph5028[,c(83:107)],2,function(x) ...
> :
> : 2) Within each of the above columns there is a single numeric code, 1, 2
or> 3 or an NA.
> :
> : 3) My goal is to determine the percent of time each person used a 2
code.> So if a person across these columns had
> : say 8 numerical entries and if 4 of these were the number 2, the answer
for> that person would be 50%.
> :
> : Best regards,
> :
> : Greg Blevins
> : The Market Solutions Group
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://www.stat.math.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide!
http://www.R-project.org/posting-guide.html>
Greg:
I think you want to apply the function Gabor suggested to the
rows of your subsetted dataframe:
mydata <- as.data.frame(matrix(c
(1, NA, 3, 2,
2, 2, 2, 2,
1, NA, 2, 2), byrow = TRUE, ncol = 4))
apply(mydata, 1, function(x){mean(x == 2, na.rm = TRUE)})
1 2 3
0.3333333 1.0000000 0.6666667
If you want percentages rather than proportions, just convert
within the function.
apply(mydata, 1, function(x){mean(x == 2, na.rm = TRUE)*100})
hope this helps,
Chuck Cleland
Greg Blevins wrote:> Here is some further clarification using a toy example.
> dataframe layout
>
> v1 v2 v3 v4
>
> Person1 1 NA 3 2
>
> Person2 2 2 2 2
>
> Person3 1 NA 2 2
>
> I am looking for the proportion of time each person used a 2 code across
the
> variables v1 to v4. For example, using the above data, the answer would
look
> as follows:
>
> Person1 33%
>
> Person2 100%
>
> Person3 66%
--
Chuck Cleland, Ph.D.
NDRI, Inc.
71 West 23rd Street, 8th floor
New York, NY 10010
tel: (212) 845-4495 (Tu, Th)
tel: (732) 452-1424 (M, W, F)
fax: (917) 438-0894
Perhaps simpler: prop2 <- rowMeans(ph5028[, 83:107] == 2, na.rm=TRUE) HTH, Andy> From: Chuck Cleland > > Greg: > I think you want to apply the function Gabor suggested to the > rows of your subsetted dataframe: > > mydata <- as.data.frame(matrix(c > (1, NA, 3, 2, > 2, 2, 2, 2, > 1, NA, 2, 2), byrow = TRUE, ncol = 4)) > > apply(mydata, 1, function(x){mean(x == 2, na.rm = TRUE)}) > > 1 2 3 > 0.3333333 1.0000000 0.6666667 > > If you want percentages rather than proportions, just convert > within the function. > > apply(mydata, 1, function(x){mean(x == 2, na.rm = TRUE)*100}) > > hope this helps, > > Chuck Cleland > > Greg Blevins wrote: > > Here is some further clarification using a toy example. > > dataframe layout > > > > v1 v2 v3 > v4 > > > > Person1 1 NA 3 2 > > > > Person2 2 2 2 > 2 > > > > Person3 1 NA 2 > 2 > > > > I am looking for the proportion of time each person used a > 2 code across the > > variables v1 to v4. For example, using the above data, the > answer would look > > as follows: > > > > Person1 33% > > > > Person2 100% > > > > Person3 66% > > -- > Chuck Cleland, Ph.D. > NDRI, Inc. > 71 West 23rd Street, 8th floor > New York, NY 10010 > tel: (212) 845-4495 (Tu, Th) > tel: (732) 452-1424 (M, W, F) > fax: (917) 438-0894 > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://www.stat.math.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html > >