lapply

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

房东的猫 提交于 2019-12-18 18:51:05
问题 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)

Print the Nth Row in a List of Data Frames

旧城冷巷雨未停 提交于 2019-12-18 12:44:14
问题 I am cleaning several excel files in R. They unfortunately are of unequal dimensions, rows and columns. Currently I am storing each excel sheet as a data frame in a list. I know how to print the 4th row of the first data frame in a list by issuing this command: df.list1[[1]][4,] Or a range of rows like this: df.list1[[1]][1:10,] My question is: How do I print a particular row for every data frame in the list? In other words: df.list1[[i]][4,] df.list1 has 30 data frames in it, but my other df

deparse(substitute(x)) in lapply?

谁都会走 提交于 2019-12-18 12:13:31
问题 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

Using rbind() to combine multiple data frames into one larger data.frame within lapply()

淺唱寂寞╮ 提交于 2019-12-18 08:46:27
问题 I'm using R-Studio 0.99.491 and R version 3.2.3 (2015-12-10). I'm a relative newbie to R, and I'd appreciate some help. I'm doing a project where I'm trying to use the server logs on an old media server to identify which folders/files within the server are still being accessed and which aren't, so that my team knows which files to migrate. Each log is for a 24 hour period, and I have approximately a year's worth of logs, so in theory, I should be able to see all of the access over the past

Error in calling `lm` in a `lapply` with `weights` argument

牧云@^-^@ 提交于 2019-12-18 07:42:53
问题 I've encounter a weird behavior when calling lm within a lapply using the weights argument. My code consist of a list of formula on which I run a linear model that I call in lapply . So far it was working: dd <- data.frame(y = rnorm(100), x1 = rnorm(100), x2 = rnorm(100), x3 = rnorm(100), x4 = rnorm(100), wg = runif(100,1,100)) ls.form <- list( formula(y~x1+x2), formula(y~x3+x4), formula(y~x1|x2|x3), formula(y~x1+x2+x3+x4) ) res.no.wg <- lapply(ls.form, lm, data = dd) However, when I add the

List of plots using lapply

纵然是瞬间 提交于 2019-12-18 03:45:10
问题 I have been using lapply and sapply as my go-to functions recently. So far so good, but why the following code does not work baffles me. df<-as.data.frame(matrix(rnorm(50),ncol=5)) names(df)<-c("x1","x2","x3","x4","x5") df1<-seq_len(10) ll<-lapply(seq(1,5), function(i) qplot(df1,df[,i])) I get the error: Error in `[.data.frame`(df, , i) : undefined columns selected Ok, apparently I made quite an unfortunate mistake in my reproducible code. It works now, but all the plots in the ll list are

Replace rbind in for-loop with lapply? (2nd circle of hell)

有些话、适合烂在心里 提交于 2019-12-17 19:48:13
问题 I am having trouble optimising a piece of R code. The following example code should illustrate my optimisation problem: Some initialisations and a function definition: a <- c(10,20,30,40,50,60,70,80) b <- c(“a”,”b”,”c”,”d”,”z”,”g”,”h”,”r”) c <- c(1,2,3,4,5,6,7,8) myframe <- data.frame(a,b,c) values <- vector(length=columns) solution <- matrix(nrow=nrow(myframe),ncol=columns+3) myfunction <- function(frame,columns){ athing = 0 if(columns == 5){ athing = 100 } else{ athing = 1000 } value[colums

getSymbols and using lapply, Cl, and merge to extract close prices

心已入冬 提交于 2019-12-17 16:41:57
问题 I've been messing around with this for some time. I recently started using the quantmod package to perform analytics on stock prices. I have a ticker vector that looks like the following: > tickers [1] "SPY" "DIA" "IWM" "SMH" "OIH" "XLY" "XLP" "XLE" "XLI" "XLB" "XLK" "XLU" "XLV" [14] "QQQ" > str(tickers) chr [1:14] "SPY" "DIA" "IWM" "SMH" "OIH" "XLY" "XLP" "XLE" ... I wrote a function called myX to use in a lapply call to save prices for every stock in the vector tickers. It has the following

passing several arguments to FUN of lapply (and others *apply)

◇◆丶佛笑我妖孽 提交于 2019-12-17 08:05:52
问题 I have a question regarding passing multiple arguments to a function, when using lapply in R . When I use lapply with the syntax of lapply(input, myfun); - this is easily understandable, and I can define myfun like that: myfun <- function(x) { # doing something here with x } lapply(input, myfun); and elements of input are passed as x argument to myfun . But what if I need to pass some more arguments to myfunc ? For example, it is defined like that: myfun <- function(x, arg1) { # doing

How to tell lapply to ignore an error and process the next thing in the list?

南楼画角 提交于 2019-12-17 06:29:40
问题 I have an example function below that reads in a date as a string and returns it as a date object. If it reads a string that it cannot convert to a date, it returns an error. testFunction <- function (date_in) { return(as.Date(date_in)) } testFunction("2010-04-06") # this works fine testFunction("foo") # this returns an error Now, I want to use lapply and apply this function over a list of dates: dates1 = c("2010-04-06", "2010-04-07", "2010-04-08") lapply(dates1, testFunction) # this works