[R] create data set from selection of rows

jim holtman jholtman at gmail.com
Thu Mar 17 13:00:44 CET 2011


Is this what you want:

> x
     V1 V2    V3 V4
1 text1 23 text2 45
2 text1 23 text3 78
3 text1 23 text3 56
4 text1 23 text2 45
> str(x)
'data.frame':   4 obs. of  4 variables:
 $ V1: Factor w/ 1 level "text1": 1 1 1 1
 $ V2: int  23 23 23 23
 $ V3: Factor w/ 2 levels "text2","text3": 1 2 2 1
 $ V4: int  45 78 56 45
> tapply(x$V4, x$V3, sum)
text2 text3
   90   134
>


On Thu, Mar 17, 2011 at 6:50 AM, e-letter <inpost at gmail.com> wrote:
> On 15/03/2011, Francisco Gochez <fjgochez at googlemail.com> wrote:
>> Hi,
>>
>> What you are after is:
>>
>> datasubset <- dataset[ dataset[,3] == "text3", ]
>
> Thank you. For the set
>
> text1,23,text2,45
> text1,23,text3,78
> text1,23,text3,56
> text1,23,text2,45
>
> Is it possible to write a function that selects rows containing
> 'text3' and applies the function 'sum' to values '78' and '56'? The
> control statements described in the document 'an introduction to r'
> (Venables and Smith, 2010) suggest that the if statement would return
> 'true', to prevent a sum function being applied to 'true' results.
>
> ______________________________________________
> 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.
>



-- 
Jim Holtman
Data Munger Guru

What is the problem that you are trying to solve?



More information about the R-help mailing list