sapply

Apply a function to each row in a data frame in R [duplicate]

给你一囗甜甜゛ 提交于 2019-12-03 04:29:56
This question already has answers here : Apply a function to every row of a matrix or a data frame (6 answers) Possible Duplicate: how to apply a function to every row of a matrix (or a data frame) in R R - how to call apply-like function on each row of dataframe with multiple arguments from each row of the df I want to apply a function to each row in a data frame, however, R applies it to each column by default. How do I force it otherwise? > a = as.data.frame(list(c(1,2,3),c(10,0,6)),header=T) > a c.1..2..3. c.10..0..6. 1 1 10 2 2 0 3 3 6 > sapply(a,min) c.1..2..3. c.10..0..6. 1 0 I wanted

R + combine a list of vectors into a single vector

丶灬走出姿态 提交于 2019-12-03 01:36:10
问题 I have a single list of numeric vector and I want to combine them into one vector. But I am unable to do that. This list can have one element common across the list element. Final vector should not add them twice. Here is an example: >lst `1` [1] 1 2 `2` [2] 2 4 5 `3` [3] 5 9 1 I want final result as this >result [1] 1 2 4 5 9 1 I tried doing following things, without worrying about the repition: >vec<-vector() >sapply(lst, append,vec) and >vec<-vector() >sapply(lst, c, vec) None of them

Repeating a user-defined function using replicate() or sapply()

纵饮孤独 提交于 2019-12-02 18:33:07
I have defined a custom function, like this: my.fun = function() { for (i in 1:1000) { ... for (j in 1:20) { ... } } return(output) } which returns an output matrix, output , composed by 1000 rows and 20 columns. What I need to do is to repeat the function say 5 times and to store the five output results into a brand new matrix, say final , but without using another for-loop (this for making the code clearer, and also because in a second moment I would like to try to parallelize these additional 5 repetitions). Hence final should be a matrix with 5000 rows and 20 columns (the rationale behind

R + combine a list of vectors into a single vector

送分小仙女□ 提交于 2019-12-02 15:24:38
I have a single list of numeric vector and I want to combine them into one vector. But I am unable to do that. This list can have one element common across the list element. Final vector should not add them twice. Here is an example: >lst `1` [1] 1 2 `2` [2] 2 4 5 `3` [3] 5 9 1 I want final result as this >result [1] 1 2 4 5 9 1 I tried doing following things, without worrying about the repition: >vec<-vector() >sapply(lst, append,vec) and >vec<-vector() >sapply(lst, c, vec) None of them worked. Can someone help me on this? Thanks. Rachit Agrawal A solution that is faster than the one proposed

How do I add rows to a data frame using sapply?

自古美人都是妖i 提交于 2019-12-02 09:52:47
I'm trying to add rows to a data frame within an sapply function call, but it's returning a matrix, whereas I want a data frame with variables in the columns and names/addresses in the rows. This is a trivial example that demonstrates the problem. I'm doing it this way to avoid using a 'for' loop. Can someone please teach me how I should be doing this? # initialize and empty data frame absdf <- data.frame( name = character(), address = character(), city = character(), stringsAsFactors=FALSE) # a numeric vector. idvs <- c(123, 465) print("initial empty data frame:") print(absdf) absdf <- sapply

How do I use arguments of a function when using sapply?

徘徊边缘 提交于 2019-12-02 08:14:16
I have a dataset which I created by column binding using the cbindX function from the gdata package. This function allows me to bind columns with different numbers of rows. So, NA 's are introduced when there are no values in a particular column. Now, I want to calculate the standard deviation for each column. I tried using sapply(dataset,sd) This returns the standard deviation for the column having all rows with values and NA for the columns having fewer rows. I tried using the na.rm argument with the sd function: sapply(dataset,sd(na.rm=T)) and got the error message Error in is.data.frame(x)

Extract JSON data from the rows of an R data frame

筅森魡賤 提交于 2019-12-02 06:30:53
问题 I have a data frame where the values of column Parameters are Json data: # Parameters #1 {"a":0,"b":[10.2,11.5,22.1]} #2 {"a":3,"b":[4.0,6.2,-3.3]} ... I want to extract the parameters of each row and append them to the data frame as columns A , B1 , B2 and B3 . How can I do it? I would rather use dplyr if it is possible and efficient. 回答1: In your example data, each row contains a json object. This format is called jsonlines aka ndjson, and the jsonlite package has a special function stream

Fill in mean values for NA in every column of a data frame [duplicate]

。_饼干妹妹 提交于 2019-12-02 04:54:46
This question already has an answer here: Replace missing values with column mean 11 answers if I have a data frame df df=data.frame(x=1:20,y=c(1:10,rep(NA,10)),z=c(rep(NA,5),1:15)) I know to replace NAs with mean value for a given column is, we can use df[is.na(df$x)]=mean(df$x,na.rm=T) What I am trying to find is a way to use a single command so that it does this for the columns at once instead of repeating it for every column. Suspecting, I need to use sapply and function, I tried something like this but clearly this does not work sapply(df,function(x) df[is.na(df$x)]=mean(df$x,na.rm=T))

How do I use arguments of a function when using sapply?

三世轮回 提交于 2019-12-02 04:35:31
问题 I have a dataset which I created by column binding using the cbindX function from the gdata package. This function allows me to bind columns with different numbers of rows. So, NA 's are introduced when there are no values in a particular column. Now, I want to calculate the standard deviation for each column. I tried using sapply(dataset,sd) This returns the standard deviation for the column having all rows with values and NA for the columns having fewer rows. I tried using the na.rm

R: How to sum pairs in a Matrix by row?

强颜欢笑 提交于 2019-12-02 02:49:30
问题 Probably this would be easy. I have a Matrix: testM <- matrix(1:40, ncol = 4, byrow = FALSE) testM [,1] [,2] [,3] [,4] [1,] 1 11 21 31 [2,] 2 12 22 32 [3,] 3 13 23 33 [4,] 4 14 24 34 [5,] 5 15 25 35 [6,] 6 16 26 36 [7,] 7 17 27 37 [8,] 8 18 28 38 [9,] 9 19 29 39 [10,] 10 20 30 40 and I want to "reduce" the matrix summing column pairs by row. Expected result: [,1] [,2] [1,] 12 52 [2,] 14 54 [3,] 16 56 [4,] 18 58 [5,] 20 60 [6,] 22 62 [7,] 24 64 [8,] 26 66 [9,] 28 68 [10,] 30 70 I tried this