r - Rename column of dataframes inside a list with its dataframe name -
i have multiple dataframes/tibbles same exact structure, different contents. name way can differentiate them. objective merge them 1 dataframe, factor column. original dataframes have 1 column each hour/measurement, first want gather everything.
imagine columns 5 11 of mtcars df hour columns.
mt1 <- mtcars mt2 <- mtcars mt3 <- mtcars mt4 <- mtcars mtlist <- list(m1 = mt1, m2 = mt2, m3 = mt3, m4 = mt4) require(tidyverse) mtlist_tidy <- lapply(mtlist, function(x){ df <- x %>% gather(exp, temp_name, 5:11) return(df) }) now i'm stuck. need rename "temp_name" column in each of dfs inside mtlist_tidy name of df i.e. m1, m2, etc:
> head(mtlist_tidy$m1) mpg cyl disp hp exp temp_name 1 21.0 6 160 110 drat 3.90 2 21.0 6 160 110 drat 3.90 3 22.8 4 108 93 drat 3.85 4 21.4 6 258 110 drat 3.08 5 18.7 8 360 175 drat 3.15 6 18.1 6 225 105 drat 2.76 should become
> head(mtlist_tidy$m1) mpg cyl disp hp exp m1 1 21.0 6 160 110 drat 3.90 2 21.0 6 160 110 drat 3.90 3 22.8 4 108 93 drat 3.85 4 21.4 6 258 110 drat 3.08 5 18.7 8 360 175 drat 3.15 6 18.1 6 225 105 drat 2.76 then purrr::reduce(mtlist_tidy, full_join) work, completing task.
i guess there must solution using purrr , skipping lapply, i'm not familiar yet package.
a couple of ideas:
first, approach problem current use map2 loop through both list , names of list simultaneously. can name new columns go list names via gather_(for standard evaluation).
map2(mtlist, names(mtlist), ~gather_(.x, "exp", .y, names(.x)[5:11]) ) note next version of purrr have imap short-cut looping through list , names of list. also, next version of tidyr use tidyeval , gather_ deprecated.
second, keep things in long format using map_df looping instead of lapply. map_df uses bind_rows @ end under hood, , can include grouping variable each list via .id argument.
mtlist %>% map_df(~.x %>% gather("exp", "temp_name", 5:11), .id = "name" ) to put dataset in wide format here can use spread. takes little more work in example because of identifying variables hp , disp have same value across multiple rows.
mtlist %>% map_df(~.x %>% gather("exp", "temp_name", 5:11), .id = "name" ) %>% group_by(name) %>% mutate( rows = 1:n() ) %>% spread(name, temp_name)
Comments
Post a Comment