Big Data/Analytics Zone is brought to you in partnership with:

Mark is a graph advocate and field engineer for Neo Technology, the company behind the Neo4j graph database. As a field engineer, Mark helps customers embrace graph data and Neo4j building sophisticated solutions to challenging data problems. When he's not with customers Mark is a developer on Neo4j and writes his experiences of being a graphista on a popular blog at http://markhneedham.com/blog. He tweets at @markhneedham. Mark is a DZone MVB and is not an employee of DZone and has posted 548 posts at DZone. You can read more from them at their website. View Full User Profile

R: Mapping Over a List of Lists

02.05.2013
| 1492 views |
  • submit to reddit

As part of the coursera Data Analysis course I had the following code to download and then read in a file:

> file <- "https://dl.dropbox.com/u/7710864/data/csv_hid/ss06hid.csv"
> download.file(file, destfile="americancommunity.csv", method="curl")
> acomm <- read.csv("americancommunity.csv")

We then had to filter the data based on the values in a couple of columns and work out how many rows were returned in each case:

> one <- acomm[acomm$RMS == 4 & !is.na(acomm$RMS) 
               & acomm$BDS == 3 & !is.na(acomm$BDS), c("RMS")]
> one
  [1] 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4
...
[137] 4 4 4 4 4 4 4 4 4 4 4 4
> two <- acomm[acomm$RMS == 5 & !is.na(acomm$RMS) 
               & acomm$BDS == 2 & !is.na(acomm$BDS), c("RMS")]
> two
  [1] 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5
...
[375] 5 5 5 5 5 5 5 5 5 5 5 5
 
> three <- acomm[acomm$RMS == 7 & !is.na(acomm$RMS) 
                 & acomm$BDS == 2 & !is.na(acomm$BDS), c("RMS")]
> three
 [1] 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7
[36] 7 7 7 7 7 7 7 7 7 7 7 7 7 7

So I needed to know how many values were in the variables onetwo and three.

I thought I could probably put those lists into another list and then use apply or one of its variants to get the length of each one.

I usually use the c function to help me create lists but it’s not helpful in this case as it creates one massive vector with all the values concatenated together:

Calling apply doesn’t have the intended outcome:

> lapply(c(one, two, three), length)
...
[[582]]
[1] 1
 
[[583]]
[1] 1

Instead what we need is the list function:

> lapply(list(one, two, three), length)
[[1]]
[1] 148
 
[[2]]
[1] 386
 
[[3]]
[1] 49

Et voila!

The code is on github as usual.

Published at DZone with permission of Mark Needham, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)