lapply

lapply / purrr::map like function that allows access to the index by default?

帅比萌擦擦* 提交于 2021-02-20 04:29:08
问题 There's a workaround to allow access the index inside a s/lapply e.g. x <- list(a=11,b=12,c=13) lapply(seq_along(x), function(y, n, i) { paste(n[[i]], y[[i]]) }, y=x, n=names(x)) Is there any function like s/lapply (or like purrr::map() ) which allows access to the index in the simplest way possible, which I guess would be to simply supply its desired name to the initial function call and nothing more; map_with_index <- function(.x, .f, index) { # Same as purrr::map() # ..but whatever string

lapply aggregate columns in multiple dataframes R

走远了吗. 提交于 2021-02-20 04:16:32
问题 I have several dataframes in a list in R. There are entries in each of those DF I would like to summarise. Im trying to get into lapply so that would be my preferred way (though if theres a better solution I would be happy to know it and why). My Sample data: df1 <- data.frame(Count = c(1,2,3), ID = c("A","A","C")) df2 <- data.frame(Count = c(1,1,2), ID = c("C","B","C")) dfList <- list(df1,df2) > head(dfList) [[1]] Count ID 1 1 A 2 2 A 3 3 C [[2]] Count ID 1 1 C 2 1 B 3 2 C I tried to

lapply aggregate columns in multiple dataframes R

試著忘記壹切 提交于 2021-02-20 04:16:08
问题 I have several dataframes in a list in R. There are entries in each of those DF I would like to summarise. Im trying to get into lapply so that would be my preferred way (though if theres a better solution I would be happy to know it and why). My Sample data: df1 <- data.frame(Count = c(1,2,3), ID = c("A","A","C")) df2 <- data.frame(Count = c(1,1,2), ID = c("C","B","C")) dfList <- list(df1,df2) > head(dfList) [[1]] Count ID 1 1 A 2 2 A 3 3 C [[2]] Count ID 1 1 C 2 1 B 3 2 C I tried to

R: asynchronous parallel lapply

隐身守侯 提交于 2021-02-19 07:14:58
问题 The simplest way I've found so far to use a parallel lapply in R was through the following example code: library(parallel) library(pbapply) cl <- makeCluster(10) clusterExport(cl = cl, {...}) clusterEvalQ(cl = cl, {...}) results <- pblapply(1:100, FUN = function(x){rnorm(x)}, cl = cl) This has a very useful feature of providing a progress bar for the results, and is very easy to reuse the same code when no parallel computations are needed, by setting cl = NULL . However, one issue that I've

extracting a dataframe from a list over many objects

旧城冷巷雨未停 提交于 2021-02-19 05:59:29
问题 I have over a 1000 objects ( z ) in R, each containing three dataframes ( df1 , df2 , df3 ) with different structures. z1$df1 … z1000$df1 z1$df2 … z1000$df2 z1$df3 … z1000$df3 I created a list of these objects (list1 thus contains z1 thru z1000) and tried to use lapply to extract one type of dataframe ( df2 ) for all objects, and then merge them to one single dataframe. Extraction: For a single object it would look like this: df15<- z15$df2 # I transferred the index of z to the extracted df I

More direct way to create list of dataframes from XML file?

生来就可爱ヽ(ⅴ<●) 提交于 2021-02-08 10:40:27
问题 SDMX (Statistical Data and Metadata Exchange) is a 'XML' grammar that defines a standard for exchanging statistical data. It uses files called Dataset Structure Definition Description (DSD) to convey the structure of a dataset. Amongst other things the DSD contains a node Codelists that is comprised of the Codelist items which in turn are parent to the Code and Name item and attribuet. I am currently trying to parse these Codelists of a DSD file requested from Eurostats REST interface into a

Error handling with lapply — output the index of failed elements

烈酒焚心 提交于 2021-02-08 03:35:29
问题 Answer to question about error handling with lapply always return NA or NULL when an element fails, i.e. myfun <- function(s) { tryCatch(doSomething(s), error = function(e) { return(NULL) } } However, this is not general enough since doSomething(s) may return NULL or NA itself. Therefore, ideally I want myfun written so that after lapply(mylist, myfun) I can somehow get all the indices of failed elements. How to do this? 回答1: Catch and release the error by handing it with identity() res =

data.table column order when using lapply and get

爱⌒轻易说出口 提交于 2021-02-07 06:12:09
问题 can someone help me understand why the two versions of the lapply operations below with and without using get() don't produce the same result? When using get() the result columns get mixed up. dt <- data.table(v1 = c(1,2), v2 = c(3,4), type = c('A', 'B')) v1 v2 type 1: 1 3 A 2: 2 4 B col_in <- c('v2', 'v1') col_out <- paste0(col_in, '.new') accessing 'type' the hard-coded way dt[, (col_out) := lapply(.SD, function(x){x * min(x[type == 'A'])}), .SDcols = col_in] produces the expected result:

data.table column order when using lapply and get

隐身守侯 提交于 2021-02-07 06:12:01
问题 can someone help me understand why the two versions of the lapply operations below with and without using get() don't produce the same result? When using get() the result columns get mixed up. dt <- data.table(v1 = c(1,2), v2 = c(3,4), type = c('A', 'B')) v1 v2 type 1: 1 3 A 2: 2 4 B col_in <- c('v2', 'v1') col_out <- paste0(col_in, '.new') accessing 'type' the hard-coded way dt[, (col_out) := lapply(.SD, function(x){x * min(x[type == 'A'])}), .SDcols = col_in] produces the expected result:

R substr function on multiple columns

懵懂的女人 提交于 2021-02-05 11:57:23
问题 I have 3 columns. First column has unique ID, second and third columns have string data and some NA data. I need to extract info from column 2 and put it in separate columns and do the same thing for column 3. I am building a function as follows, using for loops. I need to split the columns after the third letter. [For example in the V1 column below, I need to break AAAbbb as AAA and bbb and put them in separate columns. I know I can use substr to do this. I am new to R, please help. UID * V1