lapply

R - iteratively apply a function of a list of variables

让人想犯罪 __ 提交于 2019-12-01 01:13:17
My goal is to create a function that, when looped over multiple variables of a data frame, will return a new data frame containing the percents and 95% confidence intervals for each level of each variable. As an example, if I applied this function to "cyl" and "am" from the mtcars data frame, I would want this as the final result: variable level ci.95 1 cyl 4 34.38 (19.50, 53.11) 2 cyl 6 21.88 (10.35, 40.45) 3 cyl 8 43.75 (27.10, 61.94) 4 am 0 59.38 (40.94, 75.5) 5 am 1 40.62 (24.50, 59.06) So, far I have function that seems to work for a single variable; however, I have two issues that I'm

How to get the name of a data.frame within a list?

天涯浪子 提交于 2019-11-30 20:32:11
How can I get a data frame's name from a list? Sure, get() gets the object itself, but I want to have its name for use within another function. Here's the use case, in case you would rather suggest a work around: lapply(somelistOfDataframes, function(X) { ddply(X, .(idx, bynameofX), summarise, checkSum = sum(value)) }) There is a column in each data frame that goes by the same name as the data frame within the list. How can I get this name bynameofX ? names(X) would return the whole vector. EDIT: Here's a reproducible example: df1 <- data.frame(value = rnorm(100), cat = c(rep(1,50), rep(2,50))

How to apply mean function over elements of a list in R

隐身守侯 提交于 2019-11-30 19:03:53
I have a list and I want to use lapply() to compute the mean of its elements. For example, for the seventh item of the list I have: >list[[7]] [1] 1 1 1 1 1 1 1 1 1 1 and my output should be: > mean(temp[[7]][1:10]) [1] 1 But when I use lapply() like below the result would be something else. What should I do? > lapply(list[[7]][1:10],mean) [[1]] [1] 1 [[2]] [1] 1 . . . [[10]] [1] 1 To get the mean of the 7th element of the list just use mean(list[[7]]) . To get the mean of each element of the list use lapply(list,mean) . And it's a really bad idea to call your list list . Ricardo Saporta

Convert several columns from integer to numeric in R data.frame

爷,独闯天下 提交于 2019-11-30 17:56:13
I would like to convert columns from 2 to 13 (the last one) from integer to numeric. For one column, I use the following code: dades$V3 <- as.numeric(dades$V3) I want to convert columns from 2 to 13 with the same command. I create this vector: dades<-2:13 Then, how do I use lapply ? We can use lapply on the subset of dataset ( dades[2:13] ), convert to numeric and assign it back to those columns. dades[2:13] <- lapply(dades[2:13], as.numeric) 来源: https://stackoverflow.com/questions/35320991/convert-several-columns-from-integer-to-numeric-in-r-data-frame

how to determine if a character vector is a valid numeric or integer vector

☆樱花仙子☆ 提交于 2019-11-30 17:19:52
I am trying to turn a nested list structure into a dataframe. The list looks similar to the following (it is serialized data from parsed JSON read in using the httr package). myList <- list(object1 = list(w=1, x=list(y=0.1, z="cat")), object2 = list(w=NULL, x=list(z="dog"))) EDIT: my original example data was too simple. The actual data are ragged, meaning that not all variables exist for every object, and some of the list elements are NULL. I edited the data to reflect this. unlist(myList) does a great job of recursively flattening the list, and I can then use lapply to flatten all the

R extract regression coefficients from multiply regression via lapply command

一曲冷凌霜 提交于 2019-11-30 16:11:13
问题 I have a large dataset with several variables, one of which is a state variable, coded 1-50 for each state. I'd like to run a regression of 28 variables on the remaining 27 variables of the dataset (there are 55 variables total), and specific for each state. In other words, run a regression of variable1 on covariate1, covariate2, ..., covariate27 for observations where state==1. I'd then like to repeat this for variable1 for states 2-50, and the repeat the whole process for variable2,

How to prevent user from setting the end date before the start date using the Shiny dateRangeInput

烂漫一生 提交于 2019-11-30 08:52:47
问题 I have a shiny app that a user selects multiple date ranges and I would like to prevent the user from setting the end date before the start date using the dateRangeInput in the lapply function. How do I code this in R? Thanks for looking into this. Here is my code library(shiny) ui <-fluidPage( checkboxInput("add_trend", "Add Trend(s)"), conditionalPanel(condition="input.add_trend === true", numericInput("numoftrends", label="Number of Linear Trends:", min = 1, max = 10, value = 1, step = 1),

can lapply not modify variables in a higher scope

那年仲夏 提交于 2019-11-30 06:43:11
I often want to do essentially the following: mat <- matrix(0,nrow=10,ncol=1) lapply(1:10, function(i) { mat[i,] <- rnorm(1,mean=i)}) But, I would expect that mat would have 10 random numbers in it, but rather it has 0. (I am not worried about the rnorm part. Clearly there is a right way to do that. I am worry about affecting mat from within an anonymous function of lapply) Can I not affect matrix mat from inside lapply? Why not? Is there a scoping rule of R that is blocking this? Shane I discussed this issue in this related question: " Is R’s apply family more than syntactic sugar ". You will

deparse(substitute(x)) in lapply?

谁都会走 提交于 2019-11-30 06:27:04
I would like use a function that uses the standard deparse(substitute(x)) trick within lapply . Unfortunately I just get the argument of the loop back. Here's my completely useless reproducible example: # some test data a <- 5 b <- 6 li <- list(a1=a,b2=b) # my test function tf <- function(obj){ nm <- deparse(substitute(obj)) res <- list(myName=nm) res } tf(a) #returns $myName [1] "a" which is fine. If I use lapply I either get [[1L]] or the x argument of an anonymous function. lapply(li,function(x) tf(x)) # returns $a1 $a1$myName [1] "x" $b2 $b2$myName [1] "x" Is there any way to obtain the

R Shiny: How to write loop for observeEvent

♀尐吖头ヾ 提交于 2019-11-30 05:45:49
问题 I have the following code. Is there any way to write it in a loop or vectorized statement like lapply? In my real code, I have even more brushes so this will be pretty helpful. Thanks. Ignore this line. Just need to add some more texts. observeEvent(input$brush_1,{ Res=brushedPoints(D(),input$brush_1,allRows = TRUE) vals$keeprows = Res$selected_ }) observeEvent(input$brush_2,{ Res=brushedPoints(D(),input$brush_2,allRows = TRUE) vals$keeprows = Res$selected_ }) observeEvent(input$brush_3,{ Res