lapply

R: combine summary of multiple csv files into one data frame

让人想犯罪 __ 提交于 2019-12-11 03:45:14
问题 I have a directory with many csv files. I want to read them into then create a summary of a column into one data frame. getSummary<-function(df){ summary(df$Names) } filenames<-list.files(path, pattern="*.csv", full.names=T) ldf<-lapply(filenames, read.csv, header=T) results<-lapply(ldf, getSummary) It is returning results as follows: print(results) [[1]] a b c d 100 2 3 4 [[2]] a b c d 97 4 2 6 What I want is something like this filename a b c d 1.csv 100 2 3 4 2.csv 97 4 2 6 Can someone

How do I extract the index or name of the list item within FUN of lapply?

本小妞迷上赌 提交于 2019-12-11 03:14:06
问题 Say I have a list of lists lists : > lists $a [1] "fas" "asdfas" "adfaff" ... $b [1] "jnfg" "gfsdgs" "fdsafa" ... ... I'd like to export each list into its own csv file with the filename being the list index. I'm trying to do it using lapply instead of a for loop, is that possible? The difficulty for me is how do I extract the list index (or name in this case) out inside of the FUN ? For example, if I were to name them randomly I can do this: lapply(lists, function (x) write.table(paste

print list names when iterating lapply [duplicate]

大城市里の小女人 提交于 2019-12-11 03:04:30
问题 This question already has answers here : Access lapply index names inside FUN (12 answers) Closed 4 years ago . I have a time series (x,y,z and a) in a list name called dat.list. I would like to apply a function to this list using lapply . Is there a way that I can print the element names i.e., x,y,z and a after each iteration is completed in lapply. Below is the reproducible example. ## Create Dummy Data x <- ts(rnorm(40,5), start = c(1961, 1), frequency = 12) y <- ts(rnorm(50,20), start = c

Looping through a sub list, where the “sub sub” list stays constant

二次信任 提交于 2019-12-11 02:55:50
问题 I would like to loop through a sub list, where the "sub sub" list stays constant. All code mentioned by me is just the reproducible example (please note that the actual data is really large) and should work without issue. I have a list of lists wich each have 2 sub lists as follows: library(data.table) library(mice) df <- fread( "A B C D E F iso year 0 A NA 1 NA NA NLD 2009 1 Y NA 2 NA NA NLD 2009 0 Q NA 3 NA NA AUS 2011 1 NA NA 4 NA NA AUS 2011 0 0 NA 7 NA NA NLD 2008 1 1 NA 1 NA NA NLD 2008

R - Setting the class of an object created with by()

百般思念 提交于 2019-12-11 02:28:19
问题 First a little bit of context: In my package summarytools, I've defined a print method for objects of classs "summarytools". I have also created a function view() that handles objects created using by() or lapply() in such a way that the output doesn't include the lines stating the group -- or the variable in the case of lapply() ; summarytools displays its own headings containing that information, so there is some redundancy when using print . Also, the main headings are not repeated when

R: Producing several barcharts with ggplot2 and lapply: how to insert the subtitles according to a list?

烈酒焚心 提交于 2019-12-11 01:04:32
问题 My aim is to produce and save several bar plots with lapply and ggplot2 . For this Purpose, I have created a list out of my data. Now everything works fine apart from the subtitles: I would like to insert the names of the elements of my list into the graphs. So far I could only insert the name of the first element. I have found another post, which helped me a lot to get so far. I'm new here, so I hope I'm posting this question in the right way (I haven't found an option to relate to this

lapply() output as a dataframe of multiple functions - R

China☆狼群 提交于 2019-12-11 00:47:08
问题 I have been trying to create a new dataframe from several computations with lapply() . I have reached this so far reading several questions (1, 2, 3): lapply(mtcars, function(x) c(colnames(x), NROW(unique(x)), sum(is.na(x)), round(sum(is.na(x))/NROW(x),2) ) ) However, colnames(x) doesn't give the colname as x it's a vector. Second, I can't figure out a way to transform this output into a dataframe: lapply(mtcars, function(x) data.frame(NROW(unique(x)), # if I put colnames(x) here it gives an

How to assign new values from lapply to new column in dataframes in list

点点圈 提交于 2019-12-11 00:05:10
问题 I have a list of dataframes. I want to perform an operation on columns of the dataframes and then create a new column in the dataframes with the resulting new column. a <- data.frame(c(1,2,3), c(2,3,4)) b <- data.frame(c(7,8,9), c(5,6,2)) l <- list(a, b) lapply(l, function(x) x[,2]*2) What I want is for 4 6 8 and 10 12 4 to be assigned to the third columns of the first and second dataframes, respectively. This does not seem to work: lapply(l, function(x) x[,2]*2 -> x$new) 回答1: You can use

Reduce() in R over similar variable names causing error

萝らか妹 提交于 2019-12-10 20:11:14
问题 I have 19 nested lists generated from a lapply and split operation. These lists are in the form: #list1 Var col1 col2 col3 A 2 3 4 B 3 4 5 #list2 Var col1 col2 col3 A 5 6 7 B 5 4 4 ...... #list19 Var col1 col2 col3 A 3 6 7 B 7 4 4 I have been able to merge the lists with merge.all <- function(x, y) merge(x, y, all=TRUE, by="Var") out <- Reduce(merge.all, DataList) I am however getting an error due to the similarity in the names of the other columns. How can I concatenate the name of the list

Winsorizing across all columns in a data frame (R) using `lapply`

会有一股神秘感。 提交于 2019-12-10 19:03:25
问题 I am trying to apply the Winsorize() function using lapply from the library(DescTools) package. What I currently have is; data$col1 <- Winsorize(data$col1) Which essentially replaces the extreme values with a value based on quantiles, replacing the below data as follows; > data$col1 [1] -0.06775798 **-0.55213508** -0.12338265 [4] 0.04928349 **0.47524313** 0.04782829 [7] -0.05070639 **-112.67126382** 0.12657896 [10] -0.12886632 > Winsorize(data$col1) [1] -0.06775798 **-0.37884540** -0.12338265