apply

Matching timestamped data to closest time in another dataset. Properly vectorized? Faster way?

你离开我真会死。 提交于 2019-11-29 02:31:57
问题 I have a timestamp in one data frame that I am trying to match to the closest timestamp in a second dataframe, for the purpose of extracting data from the second dataframe. See below for a generic example of my approach: library(lubridate) data <- data.frame(datetime=ymd_hms(c('2015-04-01 12:23:00 UTC', '2015-04-01 13:49:00 UTC', '2015-04-01 14:06:00 UTC' ,'2015-04-01 14:49:00 UTC')), value=c(1,2,3,4)) reference <- data.frame(datetime=ymd_hms(c('2015-04-01 12:00:00 UTC', '2015-04-01 13:00:00

Vectorize() vs apply()

和自甴很熟 提交于 2019-11-29 01:45:57
The Vectorize() and the apply() functions in R can often be used to accomplish the same goal. I usually prefer vectorizing a function for readability reasons, because the main calling function is related to the task at hand while sapply is not. It is also useful to Vectorize() when I am going to be using that vectorized function multiple times in my R code. For instance: a <- 100 b <- 200 c <- 300 varnames <- c('a', 'b', 'c') getv <- Vectorize(get) getv(varnames) vs sapply(varnames, get) However, at least on SO I rarely see examples with Vectorize() in the solution, only apply() (or one of it

apply() is slow - how to make it faster or what are my alternatives?

妖精的绣舞 提交于 2019-11-28 19:38:56
I have a quite large data frame, about 10 millions of rows. It has columns x and y , and what I want is to compute hypot <- function(x) {sqrt(x[1]^2 + x[2]^2)} for each row. Using apply it would take a lot of time (about 5 minutes, interpolating from lower sizes) and memory. But it seems to be too much for me, so I've tried different things: compiling the hypot function reduces the time by about 10% using functions from plyr greatly increases the running time. What's the fastest way to do this thing? What about with(my_data,sqrt(x^2+y^2)) ? set.seed(101) d <- data.frame(x=runif(1e5),y=runif

Java collection/map apply method equivalent?

梦想的初衷 提交于 2019-11-28 18:42:42
I would like to apply a function to a Java collection, in this particular case a map. Is there a nice way to do this? I have a map and would like to just run trim() on all the values in the map and have the map reflect the updates. With Java 8's lambdas, this is a one liner: map.replaceAll((k, v) -> v.trim()); For the sake of history, here's a version without lambdas: public void trimValues(Map<?, String> map) { for (Map.Entry<?, String> e : map.entrySet()) { String val = e.getValue(); if (val != null) e.setValue(val.trim()); } } Or, more generally: interface Function<T> { T operate(T val); }

Loop over rows of dataframe applying function with if-statement

百般思念 提交于 2019-11-28 17:01:07
I'm new to R and I'm trying to sum 2 columns of a given dataframe, if both the elements to be summed satisfy a given condition. To make things clear, what I want to do is: > t.d<-as.data.frame(matrix(1:9,ncol=3)) > t.d V1 V2 V3 1 4 7 2 5 8 3 6 9 > t.d$V4<-rep(0,nrow(t.d)) > for (i in 1:nrow(t.d)){ + if (t.d$V1[i]>1 && t.d$V3[i]<9){ + t.d$V4[i]<-t.d$V1[i]+t.d$V3[i]} + } > t.d V1 V2 V3 V4 1 4 7 0 2 5 8 10 3 6 9 0 I need an efficient code, as my real dataframe has about 150000 rows and 200 columns. This gives an error: t.d$V4<-t.d$V1[t.d$V1>1]+ t.d$V3[t.d$V3>9] Is "apply" an option? I tried this:

How do I wrap a function in Javascript?

我怕爱的太早我们不能终老 提交于 2019-11-28 16:06:31
I'm writing a global error handling "module" for one of my applications. One of the features I want to have is to be able to easily wrap a function with a try{} catch{} block, so that all calls to that function will automatically have the error handling code that'll call my global logging method. (To avoid polluting the code everywhere with try/catch blocks). This is, however, slightly beyond my understanding of the low-level functioning of JavaScript, the .call and .apply methods, and the this keyword. I wrote this code, based on Prototype's Function.wrap method: Object.extend(Function

Subset dataframe such that all values in each row are less than a certain value

半世苍凉 提交于 2019-11-28 14:38:34
I have a dataframe with a dimension column and 4 value columns. How can I subset the column such that all 4 columns for each record are less than a given x? I know I could do this manually using subset and specifying the condition for each column, but is there a way to do it using maybe an apply function? Below is a sample dataframe. For example let's say the x was 0.7. In that case I would want to eliminate any rows where any column of that row is more than 0.7). zips ABC DEF GHI JKL 1 1 0.8 0.6 0.1 0.6 2 2 0.1 0.3 0.8 1.0 3 3 0.5 0.1 0.4 0.8 4 4 0.6 0.4 0.2 0.3 5 5 1.0 0.8 0.6 0.5 6 6 0.2 0

apply() not working when checking column class in a data.frame

牧云@^-^@ 提交于 2019-11-28 13:44:47
I have a dataframe. I want to inspect the class of each column. x1 = rep(1:4, times=5) x2 = factor(rep(letters[1:4], times=5)) xdat = data.frame(x1, x2) > class(xdat) [1] "data.frame" > class(xdat$x1) [1] "integer" > class(xdat$x2) [1] "factor" However, imagine that I have many columns and therefore need to use apply() to help me do the trick. But it's not working. apply(xdat, 2, class) x1 x2 "character" "character" Why cannot I use apply() to see the data type of each column? or What I should do? Thanks! You could use sapply(xdat, class) # x1 x2 # "integer" "factor" using apply would coerce

python pandas: apply a function with arguments to a series. Update

二次信任 提交于 2019-11-28 11:37:57
I would like to apply a function with argument to a pandas series: I have found two different solution of SO: python pandas: apply a function with arguments to a series and Passing multiple arguments to apply (Python) both of them rely on the use of functool.partial and they works absolutely fine. By the way the new version of Pandas support multiple argument: in any case I do not understand how does it works. Example: a=pd.DataFrame({'x':[1,2],'y':[10,20]}) a['x'].apply(lambda x,y: x+y, args=(100)) It exits with a: TypeError: <lambda>() argument after * must be a sequence, not int The