apply

apply() is giving NA values for every column

こ雲淡風輕ζ 提交于 2019-12-30 06:46:15
问题 I've been having this strange problem with apply lately. Consider the following example: set.seed(42) df <- data.frame(cars, foo = sample(LETTERS[1:5], size = nrow(cars), replace = TRUE)) head(df) speed dist foo 1 4 2 E 2 4 10 E 3 7 4 B 4 7 22 E 5 8 16 D 6 9 10 C I want to use apply to apply a function fun (say, mean ) on each column of that data.frame . If the data.frame is containing only numeric values, I do not have any problem: apply(cars, 2, mean) speed dist 15.40 42.98 But when trying

How to avoid implicit character conversion when using apply on dataframe

回眸只為那壹抹淺笑 提交于 2019-12-30 02:59:26
问题 When using apply on a data.frame, the arguments are (implicitly) converted to character. An example: df <- data.frame(v=1:10, t=1:10) df <- transform(df, t2 = as.POSIXlt(t, origin = "2013-08-13")) class(df$t2[1]) ## [1] "POSIXct" "POSIXt" (correct) but: apply(df, 1, function(y) class(y["t2"])) ## [1] "character" "character" "character" "character" "character" "character" ## [7] "character" "character" "character" "character" Is there any way to avoid this conversion? Or do I always have to

split apply recombine, plyr, data.table in R

戏子无情 提交于 2019-12-30 01:23:04
问题 I am doing the classic split-apply-recombine thing in R. My data set is a bunch of firms over time. The applying I am doing is running a regression for each firm and returning the residuals, therefore, I am not aggregating by firm. plyr is great for this but it takes a very very long time to run when the number of firms is large. Is there a way to do this with data.table ? Sample Data: dte, id, val1, val2 2001-10-02, 1, 10, 25 2001-10-03, 1, 11, 24 2001-10-04, 1, 12, 23 2001-10-02, 2, 13, 22

Matrix multiplication in scheme, List of lists

不羁岁月 提交于 2019-12-29 09:05:15
问题 I started to study Scheme and I do not understand some of it. I'm using DrRacket. I wrote the following code: (define mult_mat (λ (A B) (Trans_Mat (map (λ (x) (mul_Mat_vec A x)) (Trans_Mat B))))) That uses this functions: (define Trans_Mat (λ (A) (apply map (cons list A)))) (define mul_Mat_vec (λ (A v) (map (λ (x) (apply + (map * x v))) A))) In mult_mat , I multiply the matrix A in each vector of the transpose matrix B. It works fine. I found a code on the web that makes the multiplication in

Matrix multiplication in scheme, List of lists

折月煮酒 提交于 2019-12-29 09:05:10
问题 I started to study Scheme and I do not understand some of it. I'm using DrRacket. I wrote the following code: (define mult_mat (λ (A B) (Trans_Mat (map (λ (x) (mul_Mat_vec A x)) (Trans_Mat B))))) That uses this functions: (define Trans_Mat (λ (A) (apply map (cons list A)))) (define mul_Mat_vec (λ (A v) (map (λ (x) (apply + (map * x v))) A))) In mult_mat , I multiply the matrix A in each vector of the transpose matrix B. It works fine. I found a code on the web that makes the multiplication in

R: selecting subset without copying

允我心安 提交于 2019-12-29 04:02:31
问题 Is there a way to select a subset from objects (data frames, matrices, vectors) without making a copy of selected data? I work with quite large data sets, but never change them. However often for convenience I select subsets of the data to operate on. Making a copy of a large subset each time is very memory inefficient, but both normal indexing and subset (and thus xapply() family of functions) create copies of selected data. So I'm looking for functions or data structures that can overcome

Cumulative number of unique values in a column up to current row

十年热恋 提交于 2019-12-29 01:44:05
问题 I have a data frame, donorInfo , with donor information: id giftdate giftamt 002 2001-01-05 25.00 033 2001-05-08 50.00 054 2001-09-22 125.00 125 2001-11-05 40.00 042 2001-12-04 75.00 ... ... ... I'd like to create a column that shows the cumulative number of unique donor id's up to that date. I think it's something like: donorInfo$numUnique <- apply/lapply (donorInfo, 1, FUN=nrow(unique(donorInfo$id))) unfortunately this isn't working and I'm wondering how to remedy things. Thanks for any

How can I call a javascript constructor using call or apply? [duplicate]

余生长醉 提交于 2019-12-28 07:36:26
问题 This question already has answers here : Use of .apply() with 'new' operator. Is this possible? (36 answers) Closed 4 years ago . How could I generalise the function below to take N arguments? (Using call or apply?) Is there a programmatic way to apply arguments to 'new'? I don't want the constructor to be treated like a plain function. /** * This higher level function takes a constructor and arguments * and returns a function, which when called will return the * lazily constructed value. * *

How can I call a javascript constructor using call or apply? [duplicate]

一世执手 提交于 2019-12-28 07:36:11
问题 This question already has answers here : Use of .apply() with 'new' operator. Is this possible? (36 answers) Closed 4 years ago . How could I generalise the function below to take N arguments? (Using call or apply?) Is there a programmatic way to apply arguments to 'new'? I don't want the constructor to be treated like a plain function. /** * This higher level function takes a constructor and arguments * and returns a function, which when called will return the * lazily constructed value. * *

Applying a function to two lists?

妖精的绣舞 提交于 2019-12-28 03:01:12
问题 To find the row-wise correlation of two matrices X and Y, the output should have a correlation value for row 1 of X and row 1 of Y, ..., hence in total ten values (because there are ten rows): X <- matrix(rnorm(2000), nrow=10) Y <- matrix(rnorm(2000), nrow=10) sapply(1:10, function(row) cor(X[row,], Y[row,])) Now, how should I apply this function to two lists (containing around 50 dataframes each)? Consider list A has dataframes $1, $2, $3... and so on and list B has similar number of