I'm analyzing USA election data, candidates contribution, etc. So I got raw data from internet and trying to learn some R executing some exercises in it. This is a CSV file that I successfully loaded and analyzed with ?summary
.
I also used ?tapply
successfully, to separate candidates money contribution by state:
data_amt_st = tapply(data$contb_receipt_amt, data[c('cand_nm', 'contbr_st')], sum)
?str
(for a small sample) tells me the format of this data:
> str(data_amt_st)
num [1:3, 1:21] NA NA 451 NA NA 201 NA NA 200 NA ...
- attr(*, "dimnames")=List of 2
..$ cand_nm : chr [1:3] "Bachmann, Michele" "Obama, Barack" "Romney, Mitt"
..$ contbr_st: chr [1:21] "33" "46" "48" "7" ...
Now I need to filter out values from data_amt_st. I need states that "Obama, Barack" had more contributions than other candidates, but don't know how to do. Something with ?subset
?
Thank you very much.
EDIT 1: Attending what guys told me, about making a more concrete question: I need a list of the states where Barack Obama achieved a higher contribution level (more money) than other candidates.
EDIT 2: Trying to give you a reproducible example (is it correct?):
x = c("a", "b", "c")
y0 = c(3, 5, 1)
y1 = c(2, 1, 6)
y2 = c(4, 2, 3)
m = cbind(x, y0, y1, y2)
m
# x y0 y1 y2
# [1,] "a" "3" "2" "4"
# [2,] "b" "5" "1" "2"
# [3,] "c" "1" "6" "3"
Now, I need to know, for what y values, a
is higher than b
and c
.
Maybe
## max by column (MARGIN=2)
max_amt <- apply(data_amt_st,MARGIN=2,max,na.rm=TRUE)
data_amt_st[,max_amt==data_amt_st["Obama",]]
? (Not sure how this will work with NA
values in the Obama row: using dput
to give us a reproducible example ( http://tinyurl.com/reproducible-000 ) would be useful ...)
x <- letters[1:3]
y0 <- c(3, 5, 1)
y1 <- c(2, 1, 6)
y2 <- c(4, 2, 3)
m <- data.frame(y0, y1, y2)
rownames(m) <- x
maxvals <- apply(m,2,max,na.rm=TRUE)
which(m["a",]==maxvals) ## or
names(m)[m["a",]==maxvals]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.