0

I have a data frame consisting of 36 observations of 8 variables (the first two variables are factors and the last 6 are numeric). The structure looks like this:

Technology     Sector 2011 2012 2013 2014 2015 mean
Photovoltaic   Energy  10   20   30   10   30   20             
Wind-based     Energy  20   60   60   20   40   40  
Cultivation    Nature  10   10   20   30   30   20

I want to be get the mean for every technology, that has the string "based" in its name. I've done it that way:

df[[8]][c(grep("based",Technology))]

And it works exactly how it is supposed to. The task from my online course is, to do it also with one of the following: apply()-, lapply()-, sapply()- or tapply()-function. How can this be done?

Rio
  • 99
  • 4
  • 12
  • Possible duplicate of [Grouping functions (tapply, by, aggregate) and the \*apply family](https://stackoverflow.com/questions/3505701/grouping-functions-tapply-by-aggregate-and-the-apply-family) – De Novo Jun 23 '18 at 14:59
  • The [duplicate](https://stackoverflow.com/questions/3505701/grouping-functions-tapply-by-aggregate-and-the-apply-family/7141669#7141669) has an excellent answer that can help you with your course assignment. Good luck! – De Novo Jun 23 '18 at 15:01
  • If the task is just to subset, your way is more efficient, as it's vectorized and requires no iteration like the `*apply` functions. You con't need `c`, though; `grep` already returns a vector. – alistaire Jun 23 '18 at 15:15
  • I think that the tapply()-function is relevant, but when I write this function: tapply(df$mean,c(grep("based",Technology)),print) an error is generated, "arguments must have same length". – Rio Jun 23 '18 at 16:27

1 Answers1

1

I don't think tapply is an appropriate option because OP wants to just get a subset. Now, if it is must to use apply/lapply/sapply... then option could be as:

df$mean[mapply(function(x)grepl("based", x), df$Technology)]
#[1] 40

df$mean[sapply(df$Technology,function(x)grepl("based", x))]
#[1] 40

Data:

df <- read.table(text=  
"Technology     Sector 2011 2012 2013 2014 2015 mean
Photovoltaic   Energy  10   20   30   10   30   20             
Wind-based     Energy  20   60   60   20   40   40  
Cultivation    Nature  10   10   20   30   30   20",
header = TRUE, stringsAsFactors = FALSE)
MKR
  • 19,739
  • 4
  • 23
  • 33