[R] a question on subset a dataset

P Ehlers ehlers at math.ucalgary.ca
Thu Apr 13 12:53:33 CEST 2006


Or, possibly slightly simpler:

ok <- (x %% 2) * (y %% 2)
d[ok == 1, ]

Peter Ehlers

Petr Pikal wrote:

> Hi
> 
> I coords was data frame you could use some arithmetic to get 
> selection criteria for both numbers odd. In case of matrix you need 
> to use coords[,1]*coords[,2] instead
> 
> trunc((coords$x*coords$y)/2)!=((coords$x*coords$y)/2)
> [1]  TRUE FALSE  TRUE FALSE FALSE FALSE  TRUE FALSE  TRUE
> coords[trunc((coords$x*coords$y)/2)!=((coords$x*coords$y)/2),]
>  
> HTH
> Petr
> 
> 
> 
> On 13 Apr 2006 at 16:33, zhijie zhang wrote:
> 
> Date sent:      	Thu, 13 Apr 2006 16:33:52 +0800
> From:           	"zhijie zhang" <epistat at gmail.com>
> To:             	r-help at stat.math.ethz.ch
> Subject:        	[R] a question on subset a dataset
> 
> 
>>Dear R-users,
>> I generate a dataset "d", and want to get a subset from it.
>>**
>>*z<-rnorm(9)
>>coords<-cbind(x=c(1,1,1,2,2,2,3,3,3),y=c(1,2,3,1,2,3,1,2,3))
>>d<-SpatialPointsDataFrame(coords, data.frame (z=z[1:9]))*
>>The result*/dataset* is
>>  coordinates           z
>>1      (1, 1)  1.41173570
>>2      (1, 2)  0.18546503
>>3      (1, 3) -0.04369144
>>4      (2, 1) -0.21591338
>>5      (2, 2)  1.46377535
>>6      (2, 3)  0.22966664
>>7      (3, 1)  0.10762363
>>8      (3, 2) -1.37810256
>>9      (3, 3) - 0.96818288
>> Now I want oto get a subset with x(rows) and y(columns) being odd
>> numbers ,
>>that is , i want the subset like the following:
>>    coordinates           z
>>    (1, 1)          1.41173570
>>     (1, 3)        -0.04369144
>>     (3, 1)         0.10762363
>>    (3, 3)        -0.96818288
>>I think that the steps maybe:
>>1.decompose coords into the original x and y;
>>2.select the odd numbers of x and y;
>>3.combine the selected x and y into coordinates, including the
>>corresponding z-values. This is the subset.
>> Hope someone can solve it. I failed to get the subset.
>>
>>
>>--
>>Kind Regards,Zhi Jie,Zhang ,PHDDepartment of EpidemiologySchool of
>>Public HealthFudan UniversityTel:86-21-54237149
>>
>> [[alternative HTML version deleted]]
>>
>>______________________________________________
>>R-help at stat.math.ethz.ch mailing list
>>https://stat.ethz.ch/mailman/listinfo/r-help
>>PLEASE do read the posting guide!
>>http://www.R-project.org/posting-guide.html
> 
> 
> Petr Pikal
> petr.pikal at precheza.cz
> 
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html




More information about the R-help mailing list