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
11
votes
5 answers

Viewing all column names with any NA in R

I need to get the name of the columns that have at least 1 NA. df<-data.frame(a=1:3,b=c(NA,8,6), c=c('t',NA,7)) I need to get "b, c". I found this code: sapply(df, function(x) any(is.na(x))) But I need only the variables that have any NA. I…
GabyLP
  • 3,649
  • 7
  • 45
  • 66
10
votes
3 answers

Means multiple columns by multiple groups

I am trying to find the means, not including NAs, for multiple columns withing a dataframe by multiple groups airquality <- data.frame(City = c("CityA", "CityA","CityA", "CityB","CityB","CityB", …
Jen
  • 203
  • 1
  • 2
  • 12
10
votes
1 answer

Can vapply() be used with a variable-length FUN.VALUE?

I am trying to follow good practice and use vapply() instead of sapply() inside functions, but find the type checking from vapply() to be too inflexible when wanting a fixed length. Let's say I want something like this: list1 <- list(l1_one = 1:3,…
Ken Benoit
  • 14,454
  • 27
  • 50
10
votes
4 answers

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

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…
Stefano Lombardi
  • 1,581
  • 2
  • 22
  • 48
10
votes
2 answers

R: loop over columns in data.table

I want to determine the column classes of a large data.table. colClasses <- sapply(DT, FUN=function(x)class(x)[1]) works, but apparently local copies are stored into memory: > memory.size() [1] 687.59 > colClasses <- sapply(DT, class) >…
Martijn Tennekes
  • 1,951
  • 13
  • 19
9
votes
3 answers

apply multiple functions in sapply

I have a list of .stat files in tmp directory. sample: a.stat=> abc,10 abc,20 abc,30 b.stat=> xyz,10 xyz,30 xyz,70 and so on I need to find summary of all .stat files. Currently I am…
pythonRcpp
  • 2,042
  • 6
  • 26
  • 48
9
votes
4 answers

Extracting nth element from a nested list following strsplit - R

I've been trying to understand how to deal with the output of strsplit a bit better. I often have data such as this that I wish to split: mydata <- c("144/4/5", "154/2", "146/3/5", "142", "143/4", "DNB", "90") #[1] "144/4/5" "154/2" "146/3/5"…
jalapic
  • 13,792
  • 8
  • 57
  • 87
9
votes
3 answers

How do I count the number of words in a text (string)?

I have this string vector (for example): str <- c("this is a string current trey", "feather rtttt", "tusla", "laq") To count the number of words in this vector I used this (as given here Count the number of words in a string in R?,…
user3664020
  • 2,980
  • 6
  • 24
  • 45
9
votes
4 answers

sapply with custom function (series of if statements)

I want to run a function which looks at two vectors, returning different values depending on the signs of the values in the two vectors. I have written a function which works to compare two values, but then I want to run this on two vectors. So I…
Tom Evans
  • 553
  • 2
  • 6
  • 13
8
votes
4 answers

exists and sapply: why are these functions different?

Why are the two functions fn and gn below different? I don't think they should be, but I must be missing something. vars <- letters[1:10] a <- b <- 1 fn <- function (d) { sapply( vars, exists ) } gn <- function (d) { sapply( vars, function…
petrelharp
  • 4,829
  • 1
  • 16
  • 17
8
votes
3 answers

Create frequency tables for multiple factor columns in R

I am a novice in R. I am compiling a separate manual on the syntax for the common functions/features for my work. My sample dataframe as follows: x.sample <- structure(list(Q9_A = structure(c(5L, 3L, 5L, 3L, 5L, 3L, 1L, 5L, 5L, 5L), .Label =…
Raphael Lee
  • 111
  • 1
  • 10
8
votes
2 answers

sapply paste before at beginning of string

So I have a vector lizt <- c("a","b","c") > lizt [1] "a" "b" "c" and I can use sapply to paste characters after each element lizt2 <- sapply(lizt,paste0, "$", USE.NAMES=F) lizt2 [1] "a$" "b$" "c$" now, how do I use a similar function to paste…
dmvianna
  • 15,088
  • 18
  • 77
  • 106
7
votes
3 answers

R: fastest way to check presence of each element of a vector in each of the columns of a matrix

I have an integer vector a a=function(l) as.integer(runif(l,1,600)) a(100) [1] 414 476 6 58 74 76 45 359 482 340 103 575 494 323 74 347 157 503 385 518 547 192 149 222 152 67 497 588 388 140 457 429 353 [34] 484 91 310 394 122 302 158…
Tom Wenseleers
  • 7,535
  • 7
  • 63
  • 103
7
votes
1 answer

Using sapply on vector of POSIXct

I have what may be a very simple question. I want to process a column of POSIXct objects from a dataframe and generate a vector of datetime strings. I tried to use the following sapply call dt <- sapply(df$datetime, function(x)…
Chris
  • 3,109
  • 7
  • 29
  • 39
7
votes
1 answer

when the iterable is NOT the first argument of the function

The problem is quite simple yet I can't find the answer. I have myfun <- function(x, y). How can I sapply this function over a list of y? To apply over x I would do this iterables <- 1:10 sapply(iterables, myfun, y) But I want the iterables to be y…
Heisenberg
  • 8,386
  • 12
  • 53
  • 102
1
2
3
81 82