2

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?

phish_researcher
  • 73
  • 1
  • 1
  • 5

2 Answers2

1

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))
Metrics
  • 15,172
  • 7
  • 54
  • 83
1

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.

IRTFM
  • 258,963
  • 21
  • 364
  • 487
  • I used both approaches - they both worked. The answer from @Metrics solved my original problem. That being said, this answer also included all the 0's for dates and times that the frequency in a cell was zero - which is what I was hoping to build. – phish_researcher Aug 12 '13 at 17:09
  • The other function to consider is `xtabs`. It has a formula interface, so perhaps looking at `xtabs(cell_id ~ daterank+timerank+Period, data=dat)`. – IRTFM Aug 12 '13 at 17:52