sapply

Extract Adj R Square from a list of lm results [duplicate]

∥☆過路亽.° 提交于 2019-12-24 16:56:24
问题 This question already has answers here : Print R-squared for all of the models fit with lmList (2 answers) Closed 3 years ago . I have used 50 predictor variables to create combinations of more than 2 million regression models for a single outcome variable. Most of these are nonsense--I want to eliminate all models with an Adjusted R-Square (AR2) below 0.7, and whose members have a vif>4 (from the car package). I have been first creating a list of all of the models (b), and in a second step,

data.frame colmn mean at n-th interval of rows

半城伤御伤魂 提交于 2019-12-24 14:31:45
问题 I tried to find similar questions here but thought that it might be helpful to just create new question. I have relatively large dataset (daily weather data for several hundreds stations) I did some sliding window average calculation and I got the intermediate output like this. data frame of 10481441 row by 11 column but my row names are consist of series of numbers with identifiable prefix. For example, X1.1 X1.2 X1.3 ... X1.11659 X2.1 X2.2 X2.3 ... X2.11659 X3.1 ... X899.11659 Then what I

Transpose lists elements and match to value in R

妖精的绣舞 提交于 2019-12-24 11:03:45
问题 Here is the code written by @Ken S to extract data from OCR'd pdf, which gives a dataframe like name Status Page Words test.pdf Present test_1, test_3 gym, school test1.pdf Present test1_4, test1_7 gym, swimming pool test2.pdf Not Present - - . But I want the data to be flattened so that the output would look like fileName Status Page Words TEXT test.pdf Present test_1 gym I go gym and school regularly test.pdf Present test_1 school I go gym and school regularly test.pdf Present test_3 school

Iterate over list to get value by its name

二次信任 提交于 2019-12-24 10:59:43
问题 This works. But are there more efficient/simpler ways to get output ? test_list <- list(list("name"="A","property"=1), list("name"="B","property"=2), list("name"="C","property"=3)) myFunction <- function(arg1=NULL, arg2=NULL){ arg1[[arg2]] } # works output <- sapply(test_list, myFunction, "property") # returns NULL # output <- sapply(test_list, `$`, "property") 回答1: We can specify the anonymous function call to do the extraction sapply(test_list, function(x) x$property) #[1] 1 2 3 来源: https:/

Multiple Google Places API calls within Sapply function

时间秒杀一切 提交于 2019-12-24 08:12:48
问题 I have a list of locations that I'm feeding into the Google Places API. Some locations have more than 20 results. I'm providing an example of one such location below. To get results beyond the first 20, you have to make an additional API call to Google Places, with an extra "token" parameter that is obtained from the first Google Places API call. Using the below flawed function, I'm attempting to execute the additional API call, based on whether there are additional results that need to be

sapply cannot handle date correctly [duplicate]

老子叫甜甜 提交于 2019-12-24 07:37:20
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: Why does mapply not return date-objects? i got the following bizzar problem, I have no problem when I conver one single string to date: as.Date(alldays[1]) [1] "3-04-20" however, when I use sapply or lapply, I got a big negative number, does anyone know why it is like this? Thanks! > (sapply(alldays[1:4], as.Date)) 03-04-2012 02-04-2012 30-03-2012 29-03-2012 -718323 -718688 -708492 -708857 Hi guys, i found the

Syntax of sapply and lapply in r

廉价感情. 提交于 2019-12-24 01:27:15
问题 I have been looking into ways to separate a list in r based on the index of the sub values and I have seen this piece of code pop up a lot: sapply(myList, "[", 2) I was wondering if anyone would be able to explain this to me as the only syntax that I have used for sapply before is: sapply(myList, Function) 回答1: "[" is by itself a function. when you call sapply(LIST, "[", 2) it means extract the second element of each sub-list. The 2 which you passed goes as an argument to the function [

How do I optimize sapply in R to calculate running totals on a dataframe

六眼飞鱼酱① 提交于 2019-12-23 20:13:50
问题 I wrote a function in R to calculate cumulative totals by month number, but the execution time of my method grows exponentially as the dataset gets larger. I'm a novice R programmer, can you help me make this more efficient ? The function and the way I invoke the function: accumulate <- function(recordnum,df){ sumthese <- (df$subject == df$subject[recordnum]) & (df$month <= df$month[recordnum]) sum(df$measurement[sumthese]) } set.seed(42) datalength = 10 df <- data.frame(measurement = runif(1

How can I transform an array of characters with a few lines of code to a data.frame?

℡╲_俬逩灬. 提交于 2019-12-23 19:19:57
问题 I have the following array my_list <- c("Jan-01--Dec-31|00:00--24:00", "Jan-01--Jun-30|12:00--18:00", "Jul-06--Dec-31|09:00--19:00") What is the shortest code which results in: x1 x2 x3 1 Jan-01 Jan-01 Jul-06 2 Dec-31 Jun-30 Dec-31 and x2 x2 x3 1 00:00 12:00 09:00 2 24:00 18:00 19:00 At the moment I have the (not very nice) code df <- as.data.frame(strsplit(my_list, split = "|", fixed = T), stringsAsFactors = F) date_list <- strsplit(as.character(df[1, ]), split = "--", fixed = T) date_df <-

get the lengths of element of lists of list in R

回眸只為那壹抹淺笑 提交于 2019-12-23 19:18:55
问题 Here is my list called dico : dico <- list(list(list(c("dim.", "dimension", "dimensions", "mesures" ), c("45 cm", "45", "45 CM", "0.45m")), list(c("tamano", "volumen", "dimension", "talla"), c("45 cm", "45", "0.45 M", "45 centimiento" )), list(c("measures", "dimension", "measurement"), c("45 cm", "0.45 m", "100 inches", "100 pouces"))), list(list(c("poids", "poid", "poids net"), c("100 grammes", "100 gr", "100")), list( c("peso", "carga", "peso especifico"), c("100 gramos", "100g", "100",