0

So I'm trying to apply the summary command to a bunch of vectors and have like each one labeled with the zip code it belongs to in one output. I also one to have one is this possible? Also, I feel like there is a way to combine the first two steps but I'm not sure how. Thanks so much for the help!!!! Here's what I have so far:

 data.06511 <- subset(x, ZipCode == 6511)
 data.06513 <- subset(x, ZipCode == 6513)
 data.06515 <- subset(x, ZipCode == 6515)
 data.06516 <- subset(x, ZipCode == 6516)
 data.06519 <- subset(x, ZipCode == 6519)

 lackfood.06511 <- data.06511$lackFood12Months_Bin
 lackfood.06513 <- data.06513$lackFood12Months_Bin
 lackfood.06515 <- data.06515$lackFood12Months_Bin
 lackfood.06516 <- data.06516$lackFood12Months_Bin
 lackfood.06519 <- data.06519$lackFood12Months_Bin
 lackfood.pop <- x$lackFood12Months_Bin
  • Please provide a sample of your data and your expected output so it's easier to see what you're trying to do. See [here](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example?rq=1) for how to make a great reproducible example – MeetMrMet Apr 05 '17 at 18:13

1 Answers1

2

Here's some example code. Copy and paste it into your R session to see if it fits the bill.

dat1 <- rnorm(100)
dat2 <- rnorm(100)

data <- list(dat1, dat2) #Makes a list
lapply(data, summary) #Lapply applies the summary function to each element in a list

when I started programming, I found the apply function very difficult to understand. This might help: https://www.r-bloggers.com/using-apply-sapply-lapply-in-r/

To answer your second question about doing actions for many groups: The split command can be very helpful to split different groups of data. Try: split(mtcars, mtcars$carb)

I wanted to try combining lapply and split: lapply(split(mtcars, mtcars$carb), summary)

Also, having example data makes your question easier to answer. Hope that helps

ah bon
  • 9,293
  • 12
  • 65
  • 148
mac_staben
  • 81
  • 6