apply

Using a counter inside an apply structured loop in R

邮差的信 提交于 2019-12-01 07:54:15
I'm trying to plot from a rather complex array in R. I want to produce an image with 3 by 3 graphs, each with red and blue points on it. I've got a structure of apply loops which works, but I'd like to change the y maximum value by each row. I would normally do this using a counter, like i, in other languages. But the apply thing in R is completely baffling me! par(mfrow=c(3,3),pty="s") # a 3 by 3 graphic set.seed(1001) x <- 1:54 # with 1 to 54 along the x axis y <- array(rexp(20), dim=c(54,6,3,2)) # and the y axis coming # from an array with dimensions as shown. ymax <- c(1,0.1,0.3) # three

Why does apply(x, 1, paste0(collapse=“”) leave white space between positive values?

拥有回忆 提交于 2019-12-01 07:38:23
When I apply across columns in this example I get a white space for positive valued numbers but not for negative values? Why is this? Shouldn't paste0 remove whitespace between elements? The context behind this problem is that I am trying to form endpoints for the googlemaps directions api. library(dplyr) stop_latlon <- data.frame(lat = paste0("via:", rnorm(10)), lon = rnorm(10)) stop_latlon %>% apply(1, function(x) paste0(x, collapse = "%7")) edit: I think that it has something to do with running apply on a dataframe with different data types (lat is a character and lon is a numeric) Why does

How to apply a function on a Series

六月ゝ 毕业季﹏ 提交于 2019-12-01 07:12:40
Given a Series s : Name 0 Tennessee Oilers 1 Tennessee Titans 2 Washington Redskins I would like a apply a function to rename the values. translate = { 'Houston Oilers': 'Tennessee Titans', 'Tennessee Oilers': 'Tennessee Titans' } s = s.apply(lambda x: translate.get(x, x)) And this raises: TypeError: ("'Series' objects are mutable, thus they cannot be hashed", u'occurred at index 0') Had I applied this on DataFrame's column instead, this would have worked. I thought I was doing this according to the docs Can you correct me please? Use map to perform the lookup: In [204]: translate = { 'Houston

Applying function to consecutive subvectors of equal size

两盒软妹~` 提交于 2019-12-01 06:33:22
I am looking for a nice and fast way of applying some arbitrary function which operates on vectors, such as sum , consecutively to a subvector of consecutive K elements. Here is one simple example, which should illustrate very clearly what I want: v <- c(1, 2, 3, 4, 5, 6, 7, 8) v2 <- myapply(v, sum, group_size=3) # v2 should be equal to c(6, 15, 15) The function should try to process groups of group_size elements of a given vector and apply a function to each group (treating it as another vector). In this example, the vector v2 is obtained as follows: (1 + 2 + 3) = 6, (4 + 5 + 6) = 15, (7 + 8)

How to read in multiple data tables into R using apply function?

醉酒当歌 提交于 2019-12-01 06:29:17
I am relatively new to R and I'm having a problem reading in multiple tables from a directory using an apply function. What I would like to have the function do is to use a vector with paths to tables that I'm interested in and generate a list with objects consisting of each data frame corresponding the paths in that file. I've written the following code: f<- function(directory){ file.list <<- list.files(directory) file.paths <<- as.vector(paste(directory, file.list, sep = "/")) tables <- lapply(X = file.paths, FUN = read.table, header = TRUE,sep = "\t" )) } By my understanding, what I'm doing

Faster alternative to iterrows

南笙酒味 提交于 2019-12-01 06:27:09
I know that this topic has been addressed a thousand times. But I can't figure out a solution. I'm trying to count how often a list (each row of df1.list1) occurs in a column of list (df2.list2). All lists consist of unique values only. List1 includes about 300.000 rows and list2 30.000 rows. I've got a working code but its terribly slow (because I'm using iterrows). I also tried itertuples() but it gave me an error ("too many values to unpack (expected 2)"). I found a similar question online: Pandas counting occurrence of list contained in column of lists . In the mentioned case the person

Running sum on a column conditional on value

笑着哭i 提交于 2019-12-01 06:25:48
问题 I have a vector of binary variables which state whether a product is on promotion in the period. I'm trying to work out how to calculate the duration of each promotion and the duration between promotions. promo.flag = c(1,1,0,1,0,0,1,1,1,0,1,1,0)) So in other words: if promo.flag is same as previous period then running.total + 1 , else running.total is reset to 1 I've tried playing with apply functions and cumsum but can't manage to get the conditional reset of running total working :-( The

R: Apply function to matrix with elements of vector as argument

感情迁移 提交于 2019-12-01 05:58:50
Suppose I want to apply a function to each row of a matrix. One of the function's arguments takes a vector. I would like to apply the first element of the vector to the first row, the second element to the second row, etc. For example: set.seed(123) df<-matrix(runif(100), ncol=10) var2 <- c(1:10) MYFUNC <- function(x, Var=NA){ sum(x)/Var } I tried this: apply(df, 1, function(x) MYFUNC(x, Var=var2)) But that gives me a 10x10 matrix with the function applied to each row & Var combination, whereas I'm only interested in the diagonal elements. I also looked into the mapply function, but I'm not

Why is apply not already bound to functions in Javascript?

南楼画角 提交于 2019-12-01 05:53:36
Assume, for the sake of this question, that I want to be able to create a function in Javascript that appends all of the elements of one array to another array. One way to achieve this, if you have access to the destination array, is to say: var destination = [1,2,3]; var source = [4,5]; Array.prototype.push.apply(destination, source); console.log(destination); // [1,2,3,4,5] Now, since Array.prototype.push.apply is pretty ugly, I want to alias it to something nicer, like: var pushAll = Array.prototype.push.apply; Which I should be able to call with two arguments, the context (destination) and

Why does apply(x, 1, paste0(collapse=“”) leave white space between positive values?

依然范特西╮ 提交于 2019-12-01 05:24:39
问题 When I apply across columns in this example I get a white space for positive valued numbers but not for negative values? Why is this? Shouldn't paste0 remove whitespace between elements? The context behind this problem is that I am trying to form endpoints for the googlemaps directions api. library(dplyr) stop_latlon <- data.frame(lat = paste0("via:", rnorm(10)), lon = rnorm(10)) stop_latlon %>% apply(1, function(x) paste0(x, collapse = "%7")) edit: I think that it has something to do with