Skip to content
Back to formatted view

Raw Message

Message-ID: <1351015114.17562.YahooMailNeo@web142602.mail.bf1.yahoo.com>
Date: 2012-10-23T17:58:34Z
From: arun
Subject: How to use tapply with more than one variables grouped
In-Reply-To: <1350992511282-4647111.post@n4.nabble.com>

Hi,

Suppose if you have a threshold (say >500), then:
???? 
dat1<-read.table(text="
????????????????? AL? AR? CA? NY
Doug??? 250 250 250? NA
Jennifer? 20 340 300 100
Michele? 250 500 250? 60
Obama??? 15? 45 520 600
",header=TRUE,stringsAsFactors=FALSE,sep="")
?res<-unlist(lapply(split(dat1,rownames(dat1)),function(x) x[x[!is.na(x)]>500]))
?res
Obama.CA Obama.NY 
???? 520????? 600 


# And suppose the threshold is >400
res1<-unlist(lapply(split(dat1,rownames(dat1)),function(x) x[x[!is.na(x)]>400]))
?res1
#Michele.AR?? Obama.CA?? Obama.NY 
? # ??? 500??????? 520??????? 600 

res1[grep("Obama",names(res1))] #amount received for Obama 
#Obama.CA Obama.NY 
? # ? 520????? 600 
?length(res1[grep("Obama",names(res1))])
#[1] 2
A.K.





----- Original Message -----
From: noobmin <pseudovoid at hotmail.com>
To: r-help at r-project.org
Cc: 
Sent: Tuesday, October 23, 2012 7:41 AM
Subject: Re: [R] How to use tapply with more than one variables grouped

I believe that previously could not be understood. To facilitate'll give you
an example. Assuming my table is presented below with the amount received
from each candidate for president in a particular country state.


? ? ? ? ? ? ? ? ?  AL? AR? CA? NY
Doug? ?  250 250 250? NA
Jennifer? 20 340 300 100
Michele? 250 500 250? 60
Obama? ?  15? 45 520 600

I would like to list the states where Obama has higher amount received (ie
in CA and NY) and also the number of states, in this case 2. How to do this?

Thanks



--
View this message in context: http://r.789695.n4.nabble.com/How-to-use-tapply-with-more-than-one-variables-grouped-tp4646948p4647111.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.