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
-3
votes
1 answer

Nested for-loops into apply?

I need help figuring out how to improve a for loop. It doesn't necessarily needs to be apply, I just thought it was the best way after researching it on StackOverflow. I tried following the guides I found on StackOverflow, but I'm a newbie and…
automa7
  • 494
  • 4
  • 15
-3
votes
1 answer

R handle null string using sapply

I need to cleansing the data that contain the value of '' (a null value of string). There are two things I will do next: 1. Counting the number of null string value. 2. Replacing them with NA value However, there are some problem to accomplish…
jun wu
  • 1
  • 1
-3
votes
1 answer

apply does not result in the same vector like replaced for-loop

the function "trio" exemplifies a function with numerous calculations, that takes in certain columns from a data frame (df) and three values for the calculations: # example: data df <- data.frame(C1= c(1,2,3,4,5,6),C2= c(5,4,3,2,1,6),C3=…
user2006697
  • 1,107
  • 2
  • 11
  • 25
-3
votes
1 answer

splitting a string and extracting individual elements

First Query: File Name is Test which is a data frame and vector is only one containing names like Stacy-A, Mark-B, Giselle-C. I want to split a name lets say Stacy-A and I have more than 50000+ rows. So I want one vector containing Stacy and one…
iamashish
  • 57
  • 7
-3
votes
1 answer

how paste function working in R?

this is my code here the paste function works for only two combination.i need the same code in a loop for more than two combinations at the same time. i<-2 while (i<=10) { results<-data.frame() results<- t(apply(data,1,function(x)…
-4
votes
1 answer

All possible combinations of elements from different bins (one element from every bin)

I have a list, where each element is a set of numbers. Lengths of all sets are different: a <- list(1,c(2,3),c(4,5,6)) #> a #[[1]] #[1] 1 #[[2]] #[1] 2 3 #[[3]] #[1] 4 5 6 I'd like to get all possible combinations of one element from each set.…
Dmitry Shopin
  • 1,753
  • 10
  • 11
-5
votes
3 answers

sapply() with with vector of values instead of 1:n

I would like to have an sapply() statement of the following form: a <- c(4, 9, 20, 3, 10, 30) sapply(c(a), function(x) b[x,2] - b[x,1]) Normally instead of c(a) I would have 1:x, however now I would like to go through only the values specified in…
upabove
  • 1,057
  • 3
  • 18
  • 29
1 2 3
81
82