# select data

6 messages
Open this post in threaded view
|
Report Content as Inappropriate

## 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]] ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Open this post in threaded view
|
Report Content as Inappropriate

## Re: select data

 This was actually discussed about a week and a half ago with many good solutions offered, but I think the most idiomatic would be something like this: apply(dataset, 1, function(x) mean(x[x>0])) The reasons I like it: i) It uses the apply function to do the same operation row-wise (that's what the "1" does) to all elements of your data set -- since this is side-effect free (as a good functional language should be) it makes for easy parallelization if you move to "big data" ii) It uses an anonymous function (the "function ... " bit) which are first class objects in R and can be passed as arguments to other functions (here apply()) iii) It uses logical subscripting to pick out the values greater than zero -- I think the subscripting behavior is the very best bit of R Best, Michael On Mon, May 14, 2012 at 12:32 PM, Andrea Sica <[hidden email]> wrote: > 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]] > > ______________________________________________ > [hidden email] 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. ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Open this post in threaded view
|
Report Content as Inappropriate

## Re: select data

 In reply to this post by asica 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: [hidden email] [mailto:r-help-bounces@r- > project.org] On Behalf Of Andrea Sica > Sent: Monday, May 14, 2012 11:32 AM > To: [hidden email] > 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]] > > ______________________________________________ > [hidden email] 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. ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Open this post in threaded view
|
Report Content as Inappropriate

## Re: select data

 In reply to this post by asica On May 14, 2012, at 12:32 PM, Andrea Sica wrote: > 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 perhaps (untested in absence of reproducble example): apply(dfrm, 1, function(x) mean(x[x>=0]) ) # would also work for a   matrix object. > > Could anyone please give me the commands to do that in R? > > Thank you so much > > [[alternative HTML version deleted]] > > ______________________________________________ > [hidden email] 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. David Winsemius, MD West Hartford, CT ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Open this post in threaded view
|
Report Content as Inappropriate

## Re: select data

 Thank you all. Really! I have used the following function: apply(dfrm, 1, function(x) mean(x[x>=0]) ) Someone of you even gave me a few interesting explanations about why to use it. Still thank you all. Andrea On Mon, May 14, 2012 at 6:52 PM, David Winsemius <[hidden email]>wrote: > > On May 14, 2012, at 12:32 PM, Andrea Sica wrote: > >  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 >> > > perhaps (untested in absence of reproducble example): > > apply(dfrm, 1, function(x) mean(x[x>=0]) ) # would also work for a matrix > object. > > >> Could anyone please give me the commands to do that in R? >> >> Thank you so much >> >>        [[alternative HTML version deleted]] >> >> ______________________________**________________ >> [hidden email] 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. >> > > David Winsemius, MD > West Hartford, CT > >         [[alternative HTML version deleted]] ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.