mapply

Converting a list of lists of strings to a data frame of numbers in R

蹲街弑〆低调 提交于 2021-02-10 16:02:33
问题 I have a list of lists of strings as follows: > ll [[1]] [1] "2" "1" [[2]] character(0) [[3]] [1] "1" [[4]] [1] "1" "8" The longest list is of length 2, and I want to build a data frame with 2 columns from this list. Bonus points for also converting each item in the list to a number or NA for character(0). I have tried using mapply() and data.frame to convert to a data frame and fill with NA's as follows. # Find length of each list element len = sapply(awards2, length) # Number of NAs to fill

Sample using start and end values within a loop in R

让人想犯罪 __ 提交于 2021-02-05 07:53:36
问题 I am trying to sample between a range of values as part of a larger loop in R. As the loop progresses to each row j , I want to sample a number between the value given in the start column and the value given in the end column, placing that value in the sampled column for that row. The results should look something like this: ID start end sampled a 25 67 44 b 36 97 67 c 23 85 77 d 15 67 52 e 21 52 41 f 43 72 66 g 39 55 49 h 27 62 35 i 11 99 17 j 21 89 66 k 28 65 48 l 44 58 48 m 16 77 22 n 25

Sample using start and end values within a loop in R

拜拜、爱过 提交于 2021-02-05 07:53:12
问题 I am trying to sample between a range of values as part of a larger loop in R. As the loop progresses to each row j , I want to sample a number between the value given in the start column and the value given in the end column, placing that value in the sampled column for that row. The results should look something like this: ID start end sampled a 25 67 44 b 36 97 67 c 23 85 77 d 15 67 52 e 21 52 41 f 43 72 66 g 39 55 49 h 27 62 35 i 11 99 17 j 21 89 66 k 28 65 48 l 44 58 48 m 16 77 22 n 25

mapply with multiple arguments where one argument is constant (data)

妖精的绣舞 提交于 2021-01-28 08:54:15
问题 I'm struggling with using mapply on functions I construct where I have one or more arguments that are needed because I am programming in a bigger environment, for example if I write a function where one of the arguments are data . fun_test <- function(data,col,val1,val2){return(data[col][1,] * val1-val2)} So data and col can for example be constant, but I want to vary the output of my function depending on val1 and val2 : > mapply(FUN=fun_test,mtcars,"cyl",mtcars$cyl,mtcars$cyl*2) Error in

Applying some functions to multiple objects

吃可爱长大的小学妹 提交于 2020-11-28 02:09:31
问题 I am on Mac OS 10.10 with R 3.1.1 Suppose I have the following data frames a and b with the same attributes: a<- structure(list(X1 = 1:5, X2 = 6:10), .Names = c("X1", "X2"), row.names = c(NA, -5L), class = "data.frame") b<- structure(list(X1 = 11:15, X2 = 16:20), .Names = c("X1", "X2" ), row.names = c(NA, -5L), class = "data.frame") and suppose I need to do some cleaning/apply some functions (like dropping column and multiply the first column by 2) on both a and b at the same time such that

Applying some functions to multiple objects

拈花ヽ惹草 提交于 2020-11-28 02:09:29
问题 I am on Mac OS 10.10 with R 3.1.1 Suppose I have the following data frames a and b with the same attributes: a<- structure(list(X1 = 1:5, X2 = 6:10), .Names = c("X1", "X2"), row.names = c(NA, -5L), class = "data.frame") b<- structure(list(X1 = 11:15, X2 = 16:20), .Names = c("X1", "X2" ), row.names = c(NA, -5L), class = "data.frame") and suppose I need to do some cleaning/apply some functions (like dropping column and multiply the first column by 2) on both a and b at the same time such that

Applying some functions to multiple objects

久未见 提交于 2020-11-28 02:08:14
问题 I am on Mac OS 10.10 with R 3.1.1 Suppose I have the following data frames a and b with the same attributes: a<- structure(list(X1 = 1:5, X2 = 6:10), .Names = c("X1", "X2"), row.names = c(NA, -5L), class = "data.frame") b<- structure(list(X1 = 11:15, X2 = 16:20), .Names = c("X1", "X2" ), row.names = c(NA, -5L), class = "data.frame") and suppose I need to do some cleaning/apply some functions (like dropping column and multiply the first column by 2) on both a and b at the same time such that