[R] select data

David L Carlson dcarlson at tamu.edu
Mon May 14 18:48:51 CEST 2012


This overwrites the data so you might want to create a copy first.

example <- data.frame(V1=c(3, -1), V2=c(-2, 4), V3=c(4, 1))
tf <- ifelse(example<0, TRUE, FALSE)
example[tf] <- NA
apply(example, 1, mean, na.rm=TRUE)

----------------------------------------------
David L Carlson
Associate Professor of Anthropology
Texas A&M University
College Station, TX 77843-4352

> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
> project.org] On Behalf Of Andrea Sica
> Sent: Monday, May 14, 2012 11:32 AM
> To: r-help at r-project.org
> Subject: [R] select data
> 
> Dear all,
> 
> I am sure it won't be difficult for you!!
> I need to calculate the average among variables for the single units of
> my
> dataset.
> But, while doing it, I need to do not consider some values.
> To better explain, think like there are two units and three variables:
> 
>       V1    V2     V3
> [1]   3     -2      4
> [2]  -1      4      1
> 
> and you want to calculate the average by row, without considering those
> negative values:
> 
> => mean(1row) = (3+4)/2
> => mean(2row) = (4+1)/2
> 
> Could anyone please give me the commands to do that in R?
> 
> Thank you so much
> 
> 	[[alternative HTML version deleted]]
> 
> ______________________________________________
> 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