lapply

Apply subset function to a list of dataframes

感情迁移 提交于 2020-01-01 05:48:35
问题 I have a list of SpatialPolygonDataFrame that I can assimilate to dataframe like this: df.1 <- data.frame(A = c(1:10), B = c(1, 2, 2, 2, 5:10)) df.2 <- data.frame(A = c(1:10), B = c(1, 2, 2, 2, 2, 2, 7:10)) df.3 <- data.frame(A = c(1:10), B = c(1, 2, 2, 4:10)) list.df <- list(df.1, df.2, df.3) I would like to get a list of a subset of each dataframe based on condition ( list.df.sub is the result I am looking for): df.1.sub <- subset(df.1, df.1$B != 2) df.2.sub <- subset(df.2, df.2$B != 2) df

Why does lapply() not retain my data.table keys?

痴心易碎 提交于 2020-01-01 04:24:09
问题 I have a bunch of data.tables in a list. I want to apply unique() to each data.table in my list, but doing so destroys all my data.table keys. Here's an example: A <- data.table(a = rep(c("a","b"), each = 3), b = runif(6), key = "a") B <- data.table(x = runif(6), b = runif(6), key = "x") blah <- unique(A) Here, blah still has a key, and everything is right in the world: key(blah) # [1] "a" But if I add the data.tables to a list and use lapply() , the keys get destroyed: dt.list <- list(A, B)

Bin columns and aggregate data via random sample with replacement for iteratively larger bin sizes

泄露秘密 提交于 2019-12-31 05:50:48
问题 Below is an example matrix: mat<- matrix(c(1,0,0,0,0,0,1,0,0,0,0,0,0,0,2,0, 2,0,0,0,1,0,0,0,0,0,0,0,0,0,1,0, 0,0,1,1,1,0,0,0,0,0,0,0,0,0,0,0, 0,1,0,0,0,1,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,1,0,0,1,0,1,1,0,0,1,0,1, 1,1,0,0,0,0,0,0,1,0,1,2,1,0,0,0), nrow=16, ncol=6) dimnames(mat)<- list(c("a", "c", "f", "h", "i", "j", "l", "m", "p", "q", "s", "t", "u", "v","x", "z"), c("1", "2", "3", "4", "5", "6")) I want to group or bin columns and then aggregate data for each group. First, I would like to bin two

Convert several columns from integer to numeric in R data.frame

五迷三道 提交于 2019-12-30 06:06:54
问题 I would like to convert columns from 2 to 13 (the last one) from integer to numeric. For one column, I use the following code: dades$V3 <- as.numeric(dades$V3) I want to convert columns from 2 to 13 with the same command. I create this vector: dades<-2:13 Then, how do I use lapply ? 回答1: We can use lapply on the subset of dataset ( dades[2:13] ), convert to numeric and assign it back to those columns. dades[2:13] <- lapply(dades[2:13], as.numeric) 来源: https://stackoverflow.com/questions

write.table inside a function applied to a list of data frames overwrite outputs

心不动则不痛 提交于 2019-12-29 09:38:10
问题 I almost finish a messy code to apply several statistical methods/test to 11 data frames from different watersheds with physico-chemical parameters as variables. I reach the goal, but I need to do this functional. So to start i made a function to compute correlation, and save the results as .txt tables and .pdf images. It works great when run the function to one dataframe at the time (for that you should import each dataframe separately using read.table , which is not written in the code

Access and preserve list names in lapply function

独自空忆成欢 提交于 2019-12-28 03:31:06
问题 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.

How to index an element of a list object in R

核能气质少年 提交于 2019-12-28 02:44:08
问题 I'm doing the following in order to import some txt tables and keep them as list: # set working directory - the folder where all selection tables are stored hypo_selections<-list.files() # change object name according to each species hypo_list<-lapply(hypo_selections,read.table,sep="\t",header=T) # change object name according to each species I want to access one specific element, let's say hypo_list[1]. Since each element represents a table, how should I procced to access particular cells

All possible combinations of elements from different bins (one element from every bin) [duplicate]

安稳与你 提交于 2019-12-25 20:00:32
问题 This question already has answers here : Generate list of all possible combinations of elements of vector (6 answers) Closed 3 years ago . I have a list, where each element is a set of numbers. Lengths of all sets are different: a <- list(1,c(2,3),c(4,5,6)) #> a #[[1]] #[1] 1 #[[2]] #[1] 2 3 #[[3]] #[1] 4 5 6 I'd like to get all possible combinations of one element from each set. In this example it should be: 1 2 4, 1 2 5, 1 2 6, 1 3 4, 1 3 5, 1 3 6 I feel that some combination of *apply

R - Fast Mode Function for use in data.table[,lapply(.SD,Mode),by=.()]

我怕爱的太早我们不能终老 提交于 2019-12-25 19:06:54
问题 I'm summarizing data in a data.table, group by, where I need to take a single value of a variable in a group. I want this value to be the mode of the group. I think it needs to be mode because usually a group is 8 rows and it will have 2 rows at one value and the other 6 or so rows will be another value. Here's a simplified example, from this: key1 2 key1 2 key1 2 key1 8 key1 2 key1 2 key1 2 key1 8 I want this: key1 2 I was having trouble using the standard mode function provided by base R,

deparse(substitute(x)) in lapply

纵然是瞬间 提交于 2019-12-25 09:19:15
问题 I'm trying to create a column in multiple data frames that contain the name of the dataframe. This is so when they are appended I know where they came from. This is easy to do manually for each dataframe: # create some example data that need joining Pb = c(5, 6, 4, 5, 7) depth = c(1, 2, 3, 4, 5) df1 <- data.frame(Pb, depth) df2 <- df1 - 0.5 # add a column to each with the name of the original dataframe df1$label <- deparse( substitute(df1) ) df2$label <- deparse( substitute(df2) ) Produces