[R] New reshape2 question answered

Neotropical bat risk assessments neotropical.bats at gmail.com
Thu Aug 14 15:25:13 CEST 2014


Hi all,

Thanks all who replied. Arun, Jim and Jeff.

Seems like there are always multiple ways to achieve the same goals with R!

Jim Holtman suggested using the base functions.

Not PLYR or RESHAPE

x <- read.table(text = "Species Location Date Time...
Myochi Chena   5/26/09 18:38
	remaining data table, as.is = TRUE, header = TRUE)
# convert time to 15 minute intervals
z <- do.call(rbind, strsplit(x$Time, ":"))
mode(z) <- 'numeric'  # convert to numeric
# create 15 minute intervals
x$int15 <- floor(z %*% c(60/15, 1/15)) + 1  # make first interval 1
# create count by species
count <- do.call(rbind, lapply(split(x, x$Species), function(a){
     c(tabulate(a$int15), rep(0,96))[1:96]  # pad out to 96 values if required
     }
     )
)

percent <- t(apply(count, 1, function(a) a / sum(a) * 100))


++++++++++++++++++++++++++
Jeff Newmiller suggested I step through the problem one piece at a
time interspersed with appropriate use of the str() function to clarify
what the data looks like at each step.  Also suggested I ought to read
?DateTimeClasses and follow links from there as well.
So some homework there for me.


He suggested a better means to handling dates with this:
library(lubridate)
library(reshape2)

# set time zone to something that doesn't use daylight savings
# this may not be how your data are actually recorded... look
# up ?timezones ... the short answer is you may need to look
# at the names of some files on your system or in your R install
# directory to find out what labels correspond to your data's timezone.
Sys.setenv( TZ="Etc/GMT+5" )
dta$Dtm <- mdy_hm( paste( dta$Date, dta$Time ) )
floor5 <- function( dtm ) {

   # break up the POSIXct (number of seconds since 1/1/1970 GMT)
   dtmlt <- as.POSIXlt( dtm )
   # floor the minutes and seconds to the next lower 5 minutes
   dtmlt$sec <- 0
   dtmlt$min <- 5 * ( dtmlt$min %/% 5 )
   as.POSIXct( dtmlt )
}

dta$Dtm5 <- floor5( dta$Dtm )
# can be done with table
#table( dta$Dtm5, dta$Species )
# I prefer data frames, so reshape2 helps out
dtat <- dcast( dta, Dtm5~Species, fun.aggregate = length, value.var="Dtm5" )



More information about the R-help mailing list