Question

I have a dataframe with four variables: "Period", "cell_id", "daterank", and "timerank". I would like to get a frequency of the cell id's (there are 115 unique levels (or cell_id's)) for each date and each hour by individual Period. "Period" is a numeric identifier for an individual (5 individuals) "daterank" has values 0-29 "timerank" has values 1-24 "cell_id" are numeric identifiers for cells within a spatial grid (on a map). Example values are 101,102,103,104,105,201..205,2401..2405.

The only way I can figure how to do it so far is:

####get data by period######2051, 2483, 2507, 2627, 2723###
##tag2051##
tag2051 = subset(fr10000, Period=="2051") ###where fr10000 is the object
head(tag2051)
(d11 = subset(tag2051, daterank=="11")) 
###here, I have to go through each daterank and
timerank combination = wate of time!!
t11h2= subset(d11, timerank=="2")
t11h2
frqt11h2= table(t11h2$cell_id)
cbind(frqt11h2)

Is there a way I can get the frequency of "cell_id" for each "daterank" and each "timerank" by "Period" without having to keep changing the daterank and timerank value input manually?

Was it helpful?

Solution

Can't you just do this?

with( dat, table(cell_id, daterank, timerank, Period))

If you wanted to only get the 4 items in your comments: 2051, 2483, 2507, 2627, 2723 then just restrict the data elements to them or us an %in% phrase.

OTHER TIPS

Not tested (Date and time should be formatted before using the following function, mydata is your data)

library(plyr)
ddply(mydata,.(cell_id,daterank,timerank), transform,freq=length(cell_id))
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top