[R] noob requesting help

David Winsemius dwinsemius at comcast.net
Sun Jun 17 18:51:46 CEST 2012


On Jun 17, 2012, at 7:19 AM, capital_P wrote:

>
> Rui Barradas wrote
>>
>> It's stringsAsFactors = FALSE, just one '='.
>>
>> sapply(dat, length)
>>
>> It should return 4 times the value 34773.
>>
>> use dput()
>>
>
> it worked!
>
>> dat2 <- data.frame(dat, stringsAsFactors = FALSE)
>
>> sapply(dat2, length)
> device_info_serial               hour               time
>             34773              34773              34773
>            tripID
>             34773
>
>> dput(head(dat2, 20))
> structure(list(device_info_serial = c(121L, 121L, 121L, 121L,
> 121L, 121L, 121L, 121L, 121L, 121L, 121L, 121L, 121L, 121L, 121L,
> 121L, 121L, 121L, 121L, 121L), hour = c(10L, 11L, 11L, 11L, 11L,
> 11L, 11L, 11L, 11L, 11L, 11L, 11L, 12L, 12L, 12L, 12L, 12L, 12L,
> 12L, 13L), time = structure(c(1242896364, 1242896671, 1242897111,
> 1242897399, 1242897686, 1242897978, 1242898281, 1242898589,  
> 1242898916,
> 1242899253, 1242899542, 1242899835, 1242901122, 1242901902,  
> 1242902199,
> 1242902489, 1242902807, 1242903096, 1242903402, 1242903708), class =
> c("POSIXct",
> "POSIXt"), tzone = ""), tripID = c(3L, 3L, 3L, 3L, 3L, 3L, 3L,
> 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L)), .Names =
> c("device_info_serial",
> "hour", "time", "tripID"), row.names = c(NA, 20L), class =  
> "data.frame")
>>
>
>> departures <- lapply(split(dat2, list(dat2$device_info_serial,
>> dat2$tripID)), function(x) x[x$time == min(x$time),])

That's going to remove a lot of rows.

>
> Now I have a new problem:
>
> I need to make a histogram of the hours (in this case, their time of
> departure), for the whole population and for each bird seperately.

Since you are not telling us what columns correspond to those  
concepts, I'm going to use the terms you are. Something along the  
lines of:

require(lattice)
histogram( ~ hours | bird,  data=dat2, nint =24 , xlim=c(0,24))
histogram( ~ hours ,        data=dat2,  nint =24 , xlim=c(0,24))


>
> Before, I used this:
>
> hist(active$hour, breaks = 24, xlim=c(0,24))
>
> and:
>
> b121 <- active[which(active$device_info_serial == 121),]
> b130 <- active[which(active$device_info_serial == 130),]
> b132 <- active[which(active$device_info_serial == 132),]
> b133 <- active[which(active$device_info_serial == 133),]
> etc.
>
> But now there are no departures$device_info_serial or departures$hour,
> because departures is a list, not a data frame.
>

The result on that example was just a list with one element, a  
dataframe.

 > str(departures)
List of 1
  $ 121.3:'data.frame':	1 obs. of  4 variables:
   ..$ device_info_serial: int 121
   ..$ hour              : int 10
   ..$ time              : POSIXct[1:1], format: "2009-05-21 04:59:24"
   ..$ tripID            : int 3

So to get what you might have expected

 > dep <- departures[[1]]
 > dep
   device_info_serial hour                time tripID
1                121   10 2009-05-21 04:59:24      3


But since you didn't actually describe what you did want (or if you  
did you left upthread), its only a guess.

> I've tried:
>
>> dep <- data.frame(departures, stringsAsFactors = FALSE)
> Error in data.frame(`121.1` = list(device_info_serial = integer(0),  
> hour =
> integer(0),  :
>  arguments imply differing number of rows: 0, 1, 2

Learn to use str().

>
>> dep <- data.frame(departures)
> Error in data.frame(`121.1` = list(device_info_serial = integer(0),  
> hour =
> integer(0),  :
>  arguments imply differing number of rows: 0, 1, 2
>
> --
> View this message in context: http://r.789695.n4.nabble.com/noob-requesting-help-tp4632803p4633637.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.

David Winsemius, MD
West Hartford, CT



More information about the R-help mailing list