lapply

Use lapply to go through a list of dataframes and change class of specific column

左心房为你撑大大i 提交于 2019-12-11 08:39:17
问题 I'm trying to go through a list that has two data frames and I want to change the class of column 2 from factor to data in each of those data frames. I can solve this with a for loop, but I want to learn how to do this with lapply. tom <- data.frame(a = c(1,2,3), b = c("2017-01-09","2017-01-10","2017-09-11")) kate <- data.frame(a = c(4,5,6), b = c("2017-01-09","2017-01-10","2017-09-11")) testList <- list(tom,kate) f <- lapply(testList, function(x) { x[,2] <- as.Date(x[,2]) }) I'm looking for

Save each element of a list to text file

梦想的初衷 提交于 2019-12-11 08:30:57
问题 I have a folder with many csv files. I read them like this files <- list.files(pattern = "*.csv") [1] "ETo_1980.csv" "ETo_1981.csv" "ETo_1982.csv" "ETo_1983.csv" "ETo_1984.csv" "ETo_1985.csv" [7] "ETo_1986.csv" "ETo_1987.csv" "ETo_1988.csv" "ETo_1989.csv" "ETo_1990.csv" "ETo_1991.csv" [13] "ETo_1992.csv" "ETo_1993.csv" "ETo_1994.csv" "ETo_1995.csv" "ETo_1996.csv" "ETo_1997.csv" [19] "ETo_1998.csv" "ETo_1999.csv" "ETo_2000.csv" "ETo_2001.csv" "ETo_2002.csv" "ETo_2003.csv" [25] "ETo_2004.csv"

Dplyr applying a calculation on values in a grouping comparing each item to all _other_ items in the group

六眼飞鱼酱① 提交于 2019-12-11 07:38:50
问题 I want to work out whether a value in a grouping is different enough from other values in a grouping. Specifically I want to work out whether an end time of a matches with the start time of another lesson on the same day for the same student. Using diamonds, this is the equivalent code: library(ggplot2) diamonds %>% group_by(color, cut) %>% mutate(clash = sum( lapply( diamonds %>% filter(color == color, cut == cut, carat != carat) %$% carat, function(x) ifelse(x < carat - 0.01 && x > carat +

Join residual by group in summary Forecast table in R

你说的曾经没有我的故事 提交于 2019-12-11 06:08:14
问题 reproducible example df=structure(list(group = c(1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L), year = c(1973L, 1974L, 1975L, 1976L, 1977L, 1978L, 1973L, 1974L, 1975L, 1976L, 1977L, 1978L), Jan = c(9007L, 7750L, 8162L, 7717L, 7792L, 7836L, 9007L, 7750L, 8162L, 7717L, 7792L, 7836L), Feb = c(8106L, 6981L, 7306L, 7461L, 6957L, 6892L, 8106L, 6981L, 7306L, 7461L, 6957L, 6892L), Mar = c(8928L, 8038L, 8124L, 7767L, 7726L, 7791L, 8928L, 8038L, 8124L, 7767L, 7726L, 7791L ), Apr = c(9137L, 8422L,

Apply tidyr::separate over multiple columns

僤鯓⒐⒋嵵緔 提交于 2019-12-11 05:13:51
问题 I would like to iterate over columns in a dataframe and split them into the based on a separator. I am using tidyr::separate , which works when I do one column at a time. For example: df<- data.frame(a = c("5312,2020,1212"), b = c("345,982,284")) df <- separate(data = df, col = "a", into = paste("a", c("col1", "col2", "col3"), sep = "_"), sep = ",") Returns: a_col1 a_col2 a_col3 b 1 5312 2020 1212 345,982,284 When I try to execute the same operation over each column of df R returns an error

Looping in nlme

扶醉桌前 提交于 2019-12-11 05:05:41
问题 Hi I am trying to perform a loop function to in which a new predictor variable is used in each iteration, however I get the following error. Error in model.frame.default(formula = ~age_c + zglobcog + apoee4_carrier + : variable lengths differ (found for 'i') The data I used can obtained from following google drive spreadsheet. https://docs.google.com/spreadsheets/d/18yll44P25qsGqgZw4RPTMjlGJ0aNLCp-vYugCD7GPk8/pubhtml library(nlme) snplist <- names(mydata)[5:7] models <- lapply(snplist,

Calculate Means and Covariances for large list of dataframes, replacing loops with lapply

别说谁变了你拦得住时间么 提交于 2019-12-11 04:37:30
问题 I previously posted a question of how to create all possible combinations of a set of dataframes or the "power set" of possible data frames in this link: Creating Dataframes of all Possible Combinations without Repetition of Columns with cbind I was able to create the list of possible dataframes by first creating all possible combinations of the names of the dataframes, and storing them in Ccols , a section of which looks like this: using reduce and lapply , I then called each dataframe by

subset multiple data tables using lapply

给你一囗甜甜゛ 提交于 2019-12-11 04:17:49
问题 I have multiple data tables and all have a common column called ID . I have a vector vec that contains a set of ID values. I would like to use lapply to subset all data tables using vec I understand how to use lapply to subset the data tables but my question is how to assign the subsetted results back to original data tables Here is what I tried : tables<-c("dt1","dt2","dt3","dt4") lapply(mget(tables),function(x)x[ID %in% vec,]) The above gives subsets of all data tables but how do I assign

lapply and apply for each component and element of a list R

余生颓废 提交于 2019-12-11 04:13:59
问题 I have this list: lst lst <- list(a=c(2.5,9.8,5.0,6.7,6.5,5.2,34.4, 4.2,39.5, 1.3,0.0,0.0,4.1,0.0,0.0,25.5,196.5, 0.0,104.2,0.0,0.0,0.0,0.0,0.0),b=c(147.4,122.9,110.2,142.3)) I would like to calculate for each values of a list and for each element of a list ( a and b ) a z.score as: (x[i]-mean(x)/sd(x) , where x are all values (togheter) of each element of a list and x[i] each single component of each list element. I tried with lapply lapply(lst,function (x) as.data.frame(apply(x,2, function

matrix assignment failing within lapply

风流意气都作罢 提交于 2019-12-11 03:54:39
问题 I have a data.frame with character data, I want to end up with a matrix with the same column headings but with counts for each value. So far I can get an empty matrix of the dimensions I want, but when I try to populate myMatrix with counts, it doesn't work. myData <- data.frame(a=LETTERS[5:8], b=LETTERS[6:9], c=rep(LETTERS[5:6],2), d=rep(LETTERS[7],4)) # a b c d # 1 E F E G # 2 F G F G # 3 G H E G # 4 H I F G myValues <- sort(unique(unlist(myData))) # E F G H I myList <- lapply(myData, table