sapply

Using sapply on column with missing values

≡放荡痞女 提交于 2019-12-11 18:47:50
问题 I understand generally what the family of apply functions do, but I'm having trouble specifically with using it to mutate a new column based on another column with missing values. I'm able to accomplish my task with a for loop, but I want to speed up the performance by using apply type functions Say I have a time series of indices that start from today and end several years from now. My original indices only exist for the first few years. I then want to artificially extend these indices using

R error: Wrong length for a vector, should be 2

守給你的承諾、 提交于 2019-12-11 17:51:47
问题 Here is a gist of what I want to do: I've got 2 data frames: x (id is unique) id timestamp 282462839 2012-12-05 10:55:00 282462992 2012-12-05 12:08:00 282462740 2012-12-05 12:13:00 282462999 2012-12-05 12:48:00 y (id is not unique) id value1 value2 282462839 300 100 282462839 300 200 282462839 400 300 282462999 500 400 282462999 300 150 I also have a function myfunc(id,pvalue) that computes something and returns one of the value2 values depending on pvalue and other value1s (more complicated

How to optimize this process?

夙愿已清 提交于 2019-12-11 14:57:27
问题 I have somewhat of broad question, but I will try to make my intent as clear as possible so that people can make suggestions. I am trying to optimize a process I am doing. Generally, what I am doing is feeding a function a data frame of values and generating a prediction off of operations on specific columns. Basically a custom function that is being used with sapply (code below). What I'm doing is much to large to provide any meaningful example, so instead I will try to describe the inputs

Applying a function to multiple columns

∥☆過路亽.° 提交于 2019-12-11 12:33:55
问题 I want to apply a function to multiple columns. My data in the dataframe data is structured as follows: col1 col2 col3 x x x x x x x x x In particular, I want to apply an ADF test on the time-series of each column. I thought something like this might work: f <- function(x) ur.df(x, type = "none", lags = 10, selectlags = "AIC")) sapply(data, f) However, it seems that there's a problem handling the "variable" of the column. How is it done correctly? Update: Use this to create three columns with

How to save row names when selecting each column independently instead of row number?

妖精的绣舞 提交于 2019-12-11 09:05:25
问题 I asked this question a couple days ago and @Juan Bosco helped me and suggested the code which works perfectly and selects top n values from each column. But turns out I need the names of each selected row for each column, so in the list: "Selectedrows", I need something like "t9", "t8", "t7" instead of row number. names<- c("t1","t10","t11","t2","t3","t4","t5","t6","t7","t8","t9") values1 <- c(2,3.1,4.5,5.1,6.5,7.1,8.5,9.11,10.1,11.8,12.3) values2 <- c(1,3.1,3,5.1,6.5,7.1,8.5,9.11,10.1,12,12

R sapply loop to replace for loop

喜夏-厌秋 提交于 2019-12-11 08:44:26
问题 I have successfully switched for loops to sapply loops before, and I know for a fact (system.time()) that they are faster. BUT my mind still works in a for loop way... Please help me to convert this for loop case: names.list <- c("Anna", "Ana", "Albert", "Albort", "Rob", "Robb", "Tommy", "Tommie") misspell.list <- c("Anna", "Albort", "Robb", "Tommie") fix.list <- c("Ana", "Albert", "Rob", "Tommy") for(i in 1:length(fix.list)) { names.list[which(names.list == misspell.list[i])] <- fix.list[i]

Use apply on a multi-dimension array

心不动则不痛 提交于 2019-12-11 06:46:33
问题 A normal matrix would be 2-dimension matrix. But, I can initialise: a<-array(0,dim=c(2,3,4,5)) Which is a 2*4*5*3 matrix, or array. Command apply(a,c(2,3),sum) will give a 4*5 array, contain the sum over elements in the 1st and 4th dimension. Why it that? As far as I know, in the apply function, 1 indicates rows, 2 indicates columns, but what does 3 mean here? We need some abstraction here. 回答1: The easiest way to understand apply on an array is to try some examples. Here's some data modified

How to efficiently apply a function on a number of matrices - mean of columns

那年仲夏 提交于 2019-12-11 06:17:02
问题 So I am new to working with matrices and functions and I am trying to work out how to apply a function to calculate the column means to multiple matrices. Here is some dummy martices: A <- matrix(c(1,2,3,4,5,6,7,8,9),nrow=3) B <- matrix(c(9,8,7,6,5,4,3,2,1),nrow=3) I have 13 large matrices all different variables, but they all have the same dimensions. I want to get the mean of the columns for each individual matrices. I have worked out how to do this for an individual matrix: AA <- sapply(1

For each row extract the value in the column name that match another value in the cell

自古美人都是妖i 提交于 2019-12-11 05:51:01
问题 I have a question which can be easily solved with a for-loop. However, since I have hundred-thousands rows in a dataframe, this would take very long computational time, and thus I am looking for a quick and smart solution. For each row in my dataframe, I would like to paste the value of the cell whose column name matches the one from the first column (INDEX) The dataframe looks like this > mydata INDEX 1 2 3 4 5 6 1 2 18.9 9.5 22.6 4.7 16.2 7.4 2 2 18.9 9.5 22.6 4.7 16.2 7.4 3 2 18.9 9.5 22.6

Simple inquiry about using sapply in R

一笑奈何 提交于 2019-12-11 03:17:22
问题 I have a simple, newbie question about sapply in R. My question is about what's happening here: > sapply(1:5,function(x) matrix(x,2,2)) [,1] [,2] [,3] [,4] [,5] [1,] 1 2 3 4 5 [2,] 1 2 3 4 5 [3,] 1 2 3 4 5 [4,] 1 2 3 4 5 At first I would have thought this call to sapply would return a five element list, each element of which would have been a 2 x 2 matrix, with the value of the elements in each matrix equal to one of the values in the series passed to sapply. I now see that's exactly what