lapply

Using lapply to change column names of a list of data frames

↘锁芯ラ 提交于 2019-11-27 01:24:13
问题 I'm trying to use lapply on a list of data frames; but failing at passing the parameters correctly (I think). List of data frames: df1 <- data.frame(A = 1:10, B= 11:20) df2 <- data.frame(A = 21:30, B = 31:40) listDF <- list(df1, df2,df3) #multiple data frames w. way less columns than the length of vector todos Vector with columns names: todos <-c('col1','col2', ......'colN') I'd like to change the column names using lapply: lapply (listDF, function(x) { colnames(x)[2:length(x)] <-todos[1

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

馋奶兔 提交于 2019-11-27 00:26:49
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 fine But if I want to apply the function over a list when one string in the middle of two good dates

Loop linear regression and saving coefficients

扶醉桌前 提交于 2019-11-26 23:25:54
问题 This is part of the dataset (named "ME1") I'm using (all variables are numeric): Year AgeR rateM 1 1751 -1.0 0.241104596 2 1751 -0.9 0.036093609 3 1751 -0.8 0.011623734 4 1751 -0.7 0.006670552 5 1751 -0.6 0.006610552 6 1751 -0.5 0.008510828 7 1751 -0.4 0.009344041 8 1751 -0.3 0.011729740 9 1751 -0.2 0.010988005 10 1751 -0.1 0.015896107 11 1751 0.0 0.018190140 12 1751 0.1 0.024588340 13 1751 0.2 0.029801362 14 1751 0.3 0.044515912 15 1751 0.4 0.055240354 16 1751 0.5 0.088476758 17 1751 0.6 0

Add “filename” column to table as multiple files are read and bound

为君一笑 提交于 2019-11-26 21:40:52
问题 I have numerous csv files in multiple directories that I want to read into a R tribble or data.table. I use "list.files()" with the recursive argument set to TRUE to create a list of file names and paths, then use "lapply()" to read in multiple csv files, and then "bind_rows()" stick them all together: filenames <- list.files(path, full.names = TRUE, pattern = fileptrn, recursive = TRUE) tbl <- lapply(filenames, read_csv) %>% bind_rows() This approach works fine. However, I need to extract a

Union of intersecting vectors in a list in R

£可爱£侵袭症+ 提交于 2019-11-26 21:14:31
问题 I have a list of vectors as follows. data <- list(v1=c("a", "b", "c"), v2=c("g", "h", "k"), v3=c("c", "d"), v4=c("n", "a"), v5=c("h", "i")) I am trying to achieve the following 1) Check whether any of the vectors intersect with each other. 2) If intersecting vectors are found, get their union. So the desired output is out <- list(v1=c("a", "b", "c", "d", "n"), v2=c("g", "h", "k", "i")) I can get the union of a group of intersecting sets as follows. Reduce(union, list(data[[1]], data[[3]],

Using lapply with changing arguments

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-26 20:16:25
问题 R textbooks continue to promote the use of lapply instead of loops. This is easy even for functions with arguments like lapply(somelist, f, a=1, b=2) but what if the arguments change depending on the list element? Assume my somelist consists of: somelist$USA somelist$Europe somelist$Switzerland plus there is anotherlist with the same regions and I want use lapply with these changing arguments? This could be useful when f was a ratio calculation for example. lapply(somelist, f, a= somelist$USA

Same function over multiple data frames in R

孤街浪徒 提交于 2019-11-26 19:54:38
问题 I am new to R, and this is a very simple question. I've found a lot of similar things to what I want but not exactly it. Basically I have multiple data frames and I simply want to run the same function across all of them. A for-loop could work but I'm not sure how to set it up properly to call data frames. It also seems most prefer the lapply approach with R. I've played with the get function as well to no avail. I apologize if this is a duplicated question. Any help would be greatly

lapply vs for loop - Performance R

こ雲淡風輕ζ 提交于 2019-11-26 17:45:37
It is often said that one should prefer lapply over for loops. There are some exception as for example Hadley Wickham points out in his Advance R book. ( http://adv-r.had.co.nz/Functionals.html ) (Modifying in place, Recursion etc). The following is one of this case. Just for sake of learning, I tried to rewrite a perceptron algorithm in a functional form in order to benchmark relative performance. source ( https://rpubs.com/FaiHas/197581 ). Here is the code. # prepare input data(iris) irissubdf <- iris[1:100, c(1, 3, 5)] names(irissubdf) <- c("sepal", "petal", "species") head(irissubdf)

Faster way to read fixed-width files

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-26 14:28:52
I work with a lot of fixed width files (i.e., no separating character) that I need to read into R. So, there is usually a definition of the column width to parse the string into variables. I can use read.fwf to read in the data without a problem. However, for large files, this can take a long time. For a recent dataset, this took 800 seconds to read in a dataset with ~500,000 rows and 143 variables. seer9 <- read.fwf("~/data/rawdata.txt", widths = cols, header = FALSE, buffersize = 250000, colClasses = "character", stringsAsFactors = FALSE)) fread in the data.table package in R is awesome for

lapply-ing with the “$” function

被刻印的时光 ゝ 提交于 2019-11-26 11:38:01
问题 I was going through some examples in hadley\'s guide to functionals, and came across an unexpected problem. Suppose I have a list of model objects, x=1:3;y=3:1; bah <- list(lm(x~y),lm(y~x)) and want to extract something from each (as suggested in hadley\'s question about a list called \"trials\"). I was expecting one of these to work: lapply(bah,`$`,i=\'call\') # or... lapply(bah,`$`,call) However, these return nulls. It seems like I\'m not misusing the $ function, as these things work: `$`