49066

R data.table operations with multiple groups in single data.table and outside function with lapply

Question:

I am attempting to use this question and answer (<a href="https://stackoverflow.com/questions/24152199/r-data-table-using-lapply-on-functions-defined-outside" rel="nofollow">R data.table using lapply on functions defined outside</a>) to assist me in answering my own previously asked question (<a href="https://stackoverflow.com/questions/24940246/operations-on-unequal-sized-data-table" rel="nofollow">operations (+, -, /, *) on unequal-sized data.table</a>).

I am including a modified example below:

library(data.table) constants <- data.table(252.164401, 3785.412, 453.59237) input1a <- data.table(ID = c(37, 45, 900), a1 = c(1, 2, 3), a2 = c(43, 320, 390), b1 = c(-0.94, 2.2, -1.223), b2 = c(2.32, 4.54, 7.21), c1 = c(1, 2, 3), c2 = c(-0.94, 2.2, -1.223)) setkey(input1a, ID) dput(input1a) structure(list(ID = c(37, 45, 900), a1 = c(1, 2, 3), a2 = c(43, 320, 390), b1 = c(-0.94, 2.2, -1.223), b2 = c(2.32, 4.54, 7.21), c1 = c(1, 2, 3), c2 = c(-0.94, 2.2, -1.223)), .Names = c("ID", "a1", "a2", "b1", "b2", "c1", "c2"), row.names = c(NA, -3L), class = c("data.table", "data.frame"), .internal.selfref = <pointer: 0x39c3f38>, sorted = "ID") # input1a # ID a1 a2 b1 b2 c1 c2 # 1: 37 1 43 -0.940 2.32 1 -0.940 # 2: 45 2 320 2.200 4.54 2 2.200 # 3: 900 3 390 -1.223 7.21 3 -1.223

Based on the error message below, what needs to be changed in the next 2 lines so that both arguments "b" and "c" can be found?

fun <- function(a, b, c, wherea=parent.frame(),whereb=parent.frame(), wherec=parent.frame()) { return(get(a,wherea) - constants$constants[2] * (get(b, whereb) - get(c, wherec))) } input1a[, lapply(c('a1', 'a2', 'b1', 'b2', 'c1', 'c2'), fun, wherea=.SD, whereb=.SD, wherec=.SD), by = key(input1a)] # Error in get(b, whereb) : argument "b" is missing, with no default

This is what I would like to have for input1a:

# input1a # ID V1 V2 # 1: 37 7344.699 -12297.44 # 2: 45 -755.0824 -8537.864 # 3: 900 15988.79 -31532.38

Thank you.

<hr />

<strong>UPDATE</strong>

Based on the answer by eddi and the correction by Biogeek (constants$V2 rather than constants$constants[2]), this if the code that I am using to solve the simplified example above.

fun <- function(a, b, c) a - constants$V2 * (b - c) input1a[, lapply(1:2, function(i) fun(get(paste0('a', i)), get(paste0('b', i)), get(paste0('c', i)))), by = ID] # ID V1 V2 # 1: 37 7344.6993 -12297.443 # 2: 45 -755.0824 -8537.864 # 3: 900 15988.7949 -31532.379

Answer1:

Your function has to have arguments a, b and c, yet you're only passing it a. Thus the error.

I don't understand why you're doing the get within the function, and I would instead do:

# whatever function fun = function(a, b, c) a + b + c # evaluate inside the data.table, *then* pass it to your function input1a[, lapply(1:2, function(i) fun(get(paste0('a', i)), get(paste0('b', i)), get(paste0('c', i)))), by = ID]

Should be obvious how to change to do get within the function if you so desire.

Answer2:

I would keep it simple:

fun <- function(a1, a2, b1, b2, c1, c2) { res1 = (a1 - constants$V2 * (b1 - c1)) res2 = (a2 - constants$V2 * (b2 - c2)) return(list(res1, res2)) } # no get or apply function input1a[, fun(a1, a2, b1, b2, c1, c2), by=ID] input1a ID V1 V2 1: 37 7344.6993 -12297.443 2: 45 -755.0824 -8537.864 3: 900 15988.7949 -31532.379

Recommend

  • Using awk to sum the values of a column, based on the values of another column, append the sum and p
  • CUDA cross-compiling error! cuda-gdb error message RSEG1058
  • Subsetting a matrix using a vector of indices
  • R- joining tables using time intervals error using foverlaps
  • Android Json Parsing with Gson
  • Nutch 2.3 and HBase 1.0.0
  • Loop to create PDFs of each node in ego network with node ID as file names in R
  • Converting columns to character with sapply
  • Return control to function when modal viewcontroller dismissed
  • Get only url value of FileStack JSON.stringify?
  • r- caret package error- createDataParition no observation
  • Bayesian interval of 5 fitted values
  • R Data table setkey - error some columns are not in the data.table
  • Nightmare / Electron : Navigation Error (code - 118)
  • Get value of last non-NA row per column in data.table
  • HBase REST API Locking Rows
  • What is the difference in Angular2 between inject a provider in @Component and @Module?
  • Replace Inf in R data.table / Show number of Inf in colums
  • SSLException: Connection has been shutdown: javax.net.ssl.SSLException: Tag mismatch
  • Tips for creating scalable WPF user control
  • c# linear regression given 2 sets of data
  • JOOQ nested condition
  • Why isn't Kubernetes service DNS working?
  • Bootstrap Popover showing at wrong place upon zoom in/out or resizing Browser window
  • NHibernate proxyexception
  • Inversing an interpolation of rotation
  • How to use Windows Media Foundation with UWP without a topology
  • Sensibility of combined Maven/Ant+Ivy build management for dual platform Desktop/Android deployment?
  • joining two bezier curves
  • Bootstrap (v3.3.4) glyphicons not displayed in IE when refresh page (F5)
  • Pycharm: Marking a folder as 'sources root' is not recursive for subfolders
  • Implementation of State Monad
  • Installing iPhone App to iPhone
  • Could not find rake using whenever rails
  • Cannot resolve symbol 'MyApi'
  • Turn off referential integrity in Derby? is it possible?
  • Add sale price programmatically to product variations
  • Unable to use reactive element in my shiny app
  • How do I use LINQ to get all the Items that have a particular SubItem?