[R] subsetting tables

Eik Vettorazzi E.Vettorazzi at uke.de
Tue Sep 6 16:48:39 CEST 2011


Hi Netzwerkerin,
subset is a generic function and behaves different for different object
classes.

txt<-"  Allstar hsa.let.7a hsa.let.7a.1 hsa.let.7a.2
2    0.87       0.79        -0.57         1.07
3    0.67      -1.14        -0.78        -0.95
4   -0.46      -0.30        -0.36         1.14"
red<-read.table(textConnection(txt))
#compare
str(red[,2])
str(red[2,])

but if it comes to just counting values meeting some conditions, using
"subset" is not needed at all.

sum(red>.5)
length(which(red>.5))

and the arr.ind option of which may be useful as well.

hth

Am 06.09.2011 16:10, schrieb netzwerkerin:
> Hi guys,
>
> one of the questions where you need a real human instead of a search engine,
> so it would be great if you could help. 
>
> I have a matrix of z-scores which I would like to filter, sometimes
> columnwise, sometimes rowwise. Data looks like this:
>
>   Allstar hsa.let.7a hsa.let.7a.1 hsa.let.7a.2
> 2    0.87       0.79        -0.57         1.07
> 3    0.67      -1.14        -0.78        -0.95
> 4   -0.46      -0.30        -0.36         1.14
>
> Now I want to find all elements which are below/above some threshold. Subset
> works fine with the columns:
>
>> subset(red[,4], red[,4] > 0.5)
> [1] 1.07 1.14
>
> But not with the rows:
>
>> subset(red[2,], red[2,] > 0.5)
>   Allstar hsa.let.7a hsa.let.7a.1 hsa.let.7a.2
> 3    0.67      -1.14        -0.78        -0.95
>
> If I try to find all values above 0.5 (any row, any column, I just need the
> number of entries), this is what I try (and get):
>
>> subset(red[,], red[,] > 0.5)
>      Allstar hsa.let.7a hsa.let.7a.1 hsa.let.7a.2
> 2       0.87       0.79        -0.57         1.07
> 3       0.67      -1.14        -0.78        -0.95
> NA        NA         NA           NA           NA
> NA.1      NA         NA           NA           NA
> NA.2      NA         NA           NA           NA
>
> Obviously I'm doing something wrong, but what?
> Help very much appreciated.
> Netzwerkerin
>
> --
> View this message in context: http://r.789695.n4.nabble.com/subsetting-tables-tp3793509p3793509.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> 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.


-- 
Eik Vettorazzi Department of Medical Biometry and Epidemiology
University Medical Center Hamburg-Eppendorf Martinistr. 52 20246 Hamburg
T ++49/40/7410-58243 F ++49/40/7410-57790

--
Pflichtangaben gemäß Gesetz über elektronische Handelsregister und Genossenschaftsregister sowie das Unternehmensregister (EHUG):

Universitätsklinikum Hamburg-Eppendorf; Körperschaft des öffentlichen Rechts; Gerichtsstand: Hamburg

Vorstandsmitglieder: Prof. Dr. Jörg F. Debatin (Vorsitzender), Dr. Alexander Kirstein, Joachim Prölß, Prof. Dr. Dr. Uwe Koch-Gromus 



More information about the R-help mailing list