lapply

Error in heatmap.2 (gplots)

岁酱吖の 提交于 2019-11-29 07:41:16
Ive moved on to a new server and Installed R version 3.0 on it. (gplots library was no longer available for 2.14) Using a script that worked for version 2.14 I now encounter a problem generating a heatmap. In R version 3 I get an error: Error in lapply(args, is.character) : node stack overflow Error in dev.flush() : node stack overflow Error in par(op) : node stack overflow In R version 2.14 I get an error: Error: evaluation nested too deeply: infinite recursion / options(expressions=)? Which I can resolve by increasing the options(expressions=500000) In R version 3 increasing this option does

can lapply not modify variables in a higher scope

試著忘記壹切 提交于 2019-11-29 06:26:00
问题 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? 回答1: I

Why do rapply and lapply handle NULL differently?

ⅰ亾dé卋堺 提交于 2019-11-29 05:44:08
I'm aware that NULL values in lists can sometimes trip people up. I'm curious why in a specific instance lapply and rapply seem to treat NULL values differently. l <- list(a = 1, c = NULL, d = 3) lapply(l,is.null) $a [1] FALSE $c [1] TRUE $d [1] FALSE So far so good. How about if we do the exact same thing with rapply ? rapply(l, is.null, how = "replace") $a [1] FALSE $c list() $d [1] FALSE This example is very simple and non-recursive, but you see the same behavior in rapply with nested lists. My question is why? If, as advertised in ?rapply , it is a 'recursive version of lapply', why do

R name colnames and rownames in list of data.frames with lapply

混江龙づ霸主 提交于 2019-11-29 05:14:22
I'm pretty frustrated because I dont know how I achieve the naming of the columns and rows in a list of data.frames. I mean I want to avoid using a loop. So I figured I could use just lapply. Ok at first I have the following list: >a $nem.greedyMAP.FALSE.POS X1 X2 X3 X4 X5 X6 X7 X8 X9 X10 1 NA NA NA NA NA NA NA NA NA NA 2 NA NA NA NA NA NA NA NA NA NA 3 NA NA NA NA NA NA NA NA NA NA 4 NA NA NA NA NA NA NA NA NA NA 5 NA NA NA NA NA NA NA NA NA NA 6 NA NA NA NA NA NA NA NA NA NA 7 NA NA NA NA NA NA NA NA NA NA 8 NA NA NA NA NA NA NA NA NA NA 9 NA NA NA NA NA NA NA NA NA NA 10 NA NA NA NA NA NA

Using get inside lapply, inside a function

别说谁变了你拦得住时间么 提交于 2019-11-29 03:46:40
this may seem like a overly complicated question, but it has me driving me a little nuts for some time. It is also for curiosity, because I already have a way of doing what I need, so is not that important. In R, I need a function to return a named list object with all the arguments and the values entered by the user. For this I have made this code (toy example): foo <- function(a=1, b=5, h='coconut') { frm <- formals(foo) parms <- frm for (i in 1:length(frm)) parms[[i]] <- get(names(frm)[i]) return(parms) } So when this is asked: > foo(b=0) $a [1] 1 $b [1] 0 $h [1] "coconut" This result is

List of plots using lapply

心已入冬 提交于 2019-11-29 02:37:54
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 the same plot. When I run this: do.call(grid.arrange,ll) I get the following image: All the plots are the

Creating a named list from two vectors (names, values)

ぃ、小莉子 提交于 2019-11-28 22:06:39
问题 Is there a way to use mapply on two vectors to construct a named list? The first vector would be of type character and contain the names used for the list while the second contains the values. So far, the only solution I have is: > dummyList = list() > addToList <- function(name, value) { + dummyList[[name]] <- value + } > mapply(addToList, c("foo", "bar"), as.list(c(1, 2)) $foo `1` $bar `2` This seems like a rather contrived solution, but I can't figure out how to do it otherwise. The

Dataframes in a list; adding a new variable with name of dataframe

耗尽温柔 提交于 2019-11-28 20:40:36
I have a list of dataframes which I eventually want to merge while maintaining a record of their original dataframe name or list index. This will allow me to subset etc across all the rows. To accomplish this I would like to add a new variable 'id' to every dataframe, which contains the name/index of the dataframe it belongs to. Edit: "In my real code the dataframe variables are created from reading multiple files using the following code, so I don't have actual names only those in the 'files.to.read' list which I'm unsure if they will align with the dataframe order: mylist <- llply(files.to

lapply with “$” function

隐身守侯 提交于 2019-11-28 17:12:30
Let's say I have a list of data.frames dflist <- list(data.frame(a=1:3), data.frame(b=10:12, a=4:6)) If i want to extract the first column from each item in the list, I can do lapply(dflist, `[[`, 1) # [[1]] # [1] 1 2 3 # # [[2]] # [1] 10 11 12 Why can't I use the "$" function in the same way lapply(dflist, `$`, "a") # [[1]] # NULL # # [[2]] # NULL But these both work: lapply(dflist, function(x) x$a) `$`(dflist[[1]], "a") I realize that in this case one could use lapply(dflist, `[[`, "a") but I was working with an S4 object that didn't seem to allow indexing via [[ . For example library

Apply function on a subset of columns (.SDcols) whilst applying a different function on another column (within groups)

夙愿已清 提交于 2019-11-28 16:48:09
问题 This is very similar to a question applying a common function to multiple columns of a data.table uning .SDcols answered thoroughly here. The difference is that I would like to simultaneously apply a different function on another column which is not part of the .SD subset. I post a simple example below to show my attempt to solve the problem: dt = data.table(grp = sample(letters[1:3],100, replace = TRUE), v1 = rnorm(100), v2 = rnorm(100), v3 = rnorm(100)) sd.cols = c("v2", "v3") dt.out = dt[,