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.

1059 questions
1086
votes
10 answers

Grouping functions (tapply, by, aggregate) and the *apply family

Whenever I want to do something "map"py in R, I usually try to use a function in the apply family. However, I've never quite understood the differences between them -- how {sapply, lapply, etc.} apply the function to the input/grouped input, what…
grautur
  • 27,957
  • 33
  • 90
  • 125
139
votes
6 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
  • 122
  • 314
  • 481
59
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
  • 2,903
  • 7
  • 27
  • 54
56
votes
7 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
  • 24,365
  • 12
  • 95
  • 193
33
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
  • 3,722
  • 8
  • 29
  • 46
32
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
  • 581
  • 1
  • 6
  • 5
27
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
  • 14,815
  • 16
  • 74
  • 126
20
votes
1 answer

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,269
  • 3
  • 11
  • 30
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,354
  • 1
  • 40
  • 56
15
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
  • 14,921
  • 39
  • 121
  • 228
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
  • 4,868
  • 4
  • 35
  • 85
13
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,174
  • 1
  • 13
  • 32
11
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
  • 3,909
  • 6
  • 23
  • 62
11
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,571
  • 9
  • 25
  • 31
10
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,427
  • 2
  • 14
  • 27
1
2 3
70 71