Soheil
Soheil

Reputation: 974

Pass a column (name and value) of a data.table to the function

I want to write a simple function to calculate the mean of Var1:

data<-structure(list(time = structure(c(1358832600, 1358832600), class = c("POSIXct", 
"POSIXt"), tzone = ""), Var1 = c(0.4, 0.2)), .Names = c("time", 
"Var1"), row.names = c(NA, -2L), class = "data.frame")

data<- data.table(data)

time                Var1
2013-01-22 09:30:00 0.4
2013-01-22 09:30:00 0.2

Aggregated.Data<- function(data, col) {
 aggregated <- ddply(data, time, summarise, col= mean(eval(col)))
 return(aggregated)
}

aggregated.data <- Aggregated.Data(data, quote(Var1))
Everything works, output:

time           Col
2013-01-22 09:30:00 0.3

Questions:

  1. Is this the right way? I mean using quote and eval?
  2. Why the second column name in output is col, How can I change it to Var1?

Edit: Using data.table

Aggregated.Data<- function(data, col) {
 aggregated <- data(data, list(col=mean(eval(substitute(col)))), by=list(time=time))
 return(aggregated)
}

Upvotes: 3

Views: 1412

Answers (2)

akrun
akrun

Reputation: 887971

One option would be to use eval(as.name) and get the mean. We can change the column names later with setnames.

f1 <- function(dat, col){
 DT <- dat[, mean(eval(as.name(col))), time]
 v1 <- setdiff(colnames(DT), colnames(dat))
 setnames(DT, v1, col)
 DT 
}


f1(data, 'Var1')
#                  time Var1
#1: 2013-01-22 00:30:00  0.3

Upvotes: 4

bramtayl
bramtayl

Reputation: 4024

library(dplyr)
library(lazyeval)

summarize_column = function(data, variable_name)
  data %>% 
    group_by(time) %>% 
    select_(lazy(variable_name)) %>% 
    summarize_each(funs(mean))

data %>% summarize_column(Var1)

Upvotes: -2

Related Questions