lapply

Access and preserve list names in lapply function

丶灬走出姿态 提交于 2019-11-27 11:18:47
I need to access list names inside the lapply function. I've found some threads online where it's said I should iterate through the names of the list to be able to fetch each list element name in my function: > n = names(mylist) > mynewlist = lapply(n, function(nameindex, mylist) { return(mylist[[nameindex]]) }, mylist) > names(mynewlist) NULL > names(mynewlist) = n The problem is that mynewlist loses the original mylist indexes and I have to add that last names() assignment to restore them. Is there a way to give an explicit index name to each element returned by the lapply function? Or a

lapply with “$” function

别等时光非礼了梦想. 提交于 2019-11-27 10:16:23
问题 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

Replace first occurrence of “:” but not second in R

孤者浪人 提交于 2019-11-27 07:38:21
问题 In order to be able to process I'd like to replace the first occurrence of a : in a string (which is my marker, that a speech begins). text <- c("Mr. Mark Francois (Rayleigh) (Con): If the scheme was so poorly targeted, why were the Government about to roll it out to employees in the Department of Trade and Industry and the Department for Work and Pensions on the very day the Treasury scrapped it? The CBI and the TUC have endorsed the scheme, which has helped 500,000 people and their families

Combining lists of different lengths into data frame

纵然是瞬间 提交于 2019-11-27 07:21:20
问题 I have data like the SampleData below, which has lists of different length that I'd like to combine in to a data frame like the Desired Result below. I've tried using lapply and cbind.na from the qpcR package like the example below, but for some reason it won't let me turn the result into a data frame. If I just used two of the lists and cbind.na it will combine them and add the NA to the end like I want, but when I try using it in lapply it just leaves them as a list of different length

How to set ggplot x-label equal to variable name during lapply?

孤街醉人 提交于 2019-11-27 07:16:10
问题 I'm making plots of one y variable against multiple x variables. I have a working solution using lapply. However, I can't manage to write the name of the x variable as the x label for each plot. Here's a simplified example of what I have: The goal is to plot the y variable against each x variable resulting in three plots and adding the name of each x variable as the x axis label. Generate a dataframe with one y variable and three x variables: df <- data.frame(y.variable=c(11:20), x1=c(21:30)

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

五迷三道 提交于 2019-11-27 06:17:44
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 something here with x and arg1 } How can I use this function with passing both input elements (as x argument)

which list element is being processed when using snowfall::sfLapply?

旧城冷巷雨未停 提交于 2019-11-27 06:03:39
问题 Assume we have a list ( mylist ) that is use as input object for a lapply function. Is there a way to know which element in mylist is being evaluated? The method should work on lapply and snowfall::sfApply (and possible others apply family members) as well. On chat, Gavin Simpson suggested the following method. This works great for lapply but not so much for sfApply . I would like to avoid extra packages or fiddling with the list. Any suggestions? mylist <- list(a = 1:10, b = 1:10) foo <-

Using lapply to apply a function over list of data frames and saving output to files with different names

ぃ、小莉子 提交于 2019-11-27 05:25:16
问题 I have a list of data frames and have given each element in the list (e.g. each data frame) a name: e.g. df1 <- data.frame(x = c(1:5), y = c(11:15)) df2 <- data.frame(x = c(1:5), y = c(11:15)) mylist <- list(A = df1, B = df2) I have a function that I want to apply to each data frame; In this function, I want to include a line to write the results to file (eventually I want to do more complicated things like save plots of the correlation between two variables for each data frame but thought I

Writing multiple data frames into .csv files using R

*爱你&永不变心* 提交于 2019-11-27 04:43:36
I have used lapply to apply a function to a number of data frames: data.cleaned <- lapply(data.list, shooter_cleaning) And then labeled each of the resulting data frames in the list according to their subject number (e.g., 100): names(data.cleaned) <- subject.names What I want to do is to save each new data frame as an individual .csv file based on its subject number. For example, for subject 100 I'd like the .csv file to be labeled as "100.csv" Normally to do this (for a single data frame) I would just write (where x is the data frame): write.csv(x, "100.csv", row.names = F) But, obviously

Rename Columns of Data.frame in list

岁酱吖の 提交于 2019-11-27 03:40:00
问题 I am trying to use lapply (and want the solution with lapply) to rename columns of a data.frame located in a list, but it's returning names, not the renamed data.frames: # define list li <- list(u_n = data.frame(x = 1:3), r_l = data.frame(y = 4:6)) # trying to rename columns after the element of the list they're located in li_2 <- lapply(1:length(li), function(x,y) colnames(y[[x]]) <- names(y)[x], y = li) However, this returns: [[1]] [1] "u_n" [[2]] [1] "r_l" If I use the same method as the