Questions tagged [sapply]

sapply is a command in the R language that applies a function to each element of a vector (atomic or list). It may also accept other classes if they are coercible by the function base::as.list. The sapply function returns a vector by default, however will return a list when more suitable or an array if argument simplify = "array" is specified.

sapply is a command in the R language that applies a function to each element of a list. The sapply function returns a vector, rather than a list.

1222 questions
159
votes
7 answers

Apply a function to every row of a matrix or a data frame

Suppose I have a n by 2 matrix and a function that takes a 2-vector as one of its arguments. I would like to apply the function to each row of the matrix and get a n-vector. How to do this in R? For example, I would like to compute the density of a…
Tim
  • 1
  • 141
  • 372
  • 590
63
votes
6 answers

R + combine a list of vectors into a single vector

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…
Rachit Agrawal
  • 3,203
  • 10
  • 32
  • 56
62
votes
8 answers

Apply function to each column in a data frame observing each columns existing data type

I'm trying to get the min/max for each column in a large data frame, as part of getting to know my data. My first try was: apply(t,2,max,na.rm=1) It treats everything as a character vector, because the first few columns are character types. So max…
Darren Cook
  • 27,837
  • 13
  • 117
  • 217
41
votes
7 answers

How to subset from a list in R

I have a rather simple task but haven't find a good solution. > mylist [[1]] [1] 1 2 3 4 5 6 7 8 9 10 [[2]] [1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o" "p" "q" "r" "s" "t" "u" "v" "w" "x" "y" "z" [[3]] [1] 25 26 27…
user1257894
  • 671
  • 1
  • 7
  • 5
38
votes
3 answers

Concatenate row-wise across specific columns of dataframe

I have a data frame with columns that, when concatenated (row-wise) as a string, would allow me to partition the data frame into a desired form. > str(data) 'data.frame': 680420 obs. of 10 variables: $ A : chr "2011-01-26"…
Jubbles
  • 4,450
  • 8
  • 35
  • 47
29
votes
1 answer

Apply a function to each row in a data frame in R

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…
highBandWidth
  • 16,751
  • 20
  • 84
  • 131
21
votes
2 answers

Extract second subelement of every element in a list while ignoring NA's in sapply in R

I'm trying to extract the second subelement of every element in a list while ignoring NAs in R. Here's a small example: mylist <- list(a=c(6,7),b=NA,c=c(8,9)) sapply(mylist, "[[", 1) sapply(mylist, "[[", 2) #receive error Because element 'b' has…
itpetersen
  • 1,475
  • 3
  • 13
  • 32
19
votes
4 answers

Using "..." and "replicate"

In the documentation of sapply and replicate there is a warning regarding using ... Now, I can accept it as such, but would like to understand what is behind it. So I've created this little contrived example: innerfunction<-function(x, extrapar1=0,…
Nick Sabbe
  • 11,684
  • 1
  • 43
  • 57
18
votes
1 answer

How to pass na.rm=TRUE to sapply when calculating median?

I have created a dataframe "killers" with 3 variables. The data are numeric though there exist NA values throughout. My goal is to calculate the mean on each of the 3 variables. sapply(killers, function(x) median) This…
Doug Fir
  • 19,971
  • 47
  • 169
  • 299
15
votes
3 answers

R: Apply function on specific columns preserving the rest of the dataframe

I'd like to learn how to apply functions on specific columns of my dataframe without "excluding" the other columns from my df. For example i'd like to multiply some specific columns by 1000 and leave the other ones as they are. Using the sapply…
Joschi
  • 2,941
  • 9
  • 28
  • 36
14
votes
2 answers

Convert a list of lists to a character vector

I have a list of lists of characters. For example: l <- list(list("A"),list("B"),list("C","D")) So as you can see some elements are lists of length > 1. I want to convert this list of lists to a character vector, but I'd like the lists with length…
dan
  • 6,048
  • 10
  • 57
  • 125
14
votes
2 answers

Dollar operator as function argument for sapply not working as expected

I have the following list test_list=list(list(a=1,b=2),list(a=3,b=4)) and I want to extract all elements with list element name a. I can do this via sapply(test_list,`[[`,"a") which gives me the correct result #[1] 1 3 When I try the same with Rs…
cryo111
  • 4,444
  • 1
  • 15
  • 37
13
votes
1 answer

in R dplyr why do I need to ungroup() after I count()?

When I first started programming in R I would often use dplyr count(). library(tidyverse) mtcars %>% count(cyl) Once I started using apply functions I started running into issues with count(). If I simply added ungroup() to the end of my…
stackinator
  • 5,429
  • 8
  • 43
  • 84
11
votes
3 answers

How to get the last element in a list in R

I want to get the every last element in a list. I have found that by using sapply function, the first element can be obtained. sapply(a,`[`,1) However, I don't actually understantd what is the meaning of [, and how to get the last element in a…
Roy
  • 539
  • 1
  • 4
  • 12
11
votes
2 answers

apply, sapply and lappy return NULL

I have a matrix: mat <- matrix(c(0,0,0,0,1,1,1,1,-1,-1,-1,-1), ncol = 4 , nrow = 4) and I apply the following functions to filter out the columns with only positive entries, but for the columns that have negative entries I get a NULL. How can I…
Cauchy
  • 1,677
  • 2
  • 21
  • 30
1
2 3
81 82