简体   繁体   中英

How to use a variable as part of the new column name using mutate in R?

I am currently working on a project where I am using a while statement to create variables in a new data frame using mutate() The code I have come up with so far looks like this:

WA_Cases_d_Day <- WA_Cases
i <- 2
while (i < ncol(WA_Cases)) {
  WA_Cases_d_Day <- WA_Cases_d_Day %>%
    mutate(d_daily = (WA_Cases[(i + 1)] - WA_Cases[i]))
  #print(i) test to ensure while works correctly
  i <- i + 1
}

The problem I am running into is it is only creating 1 variable named d_daily. To fix this I was trying to find a way to append a variable to the end of my new variable name call in the mutate function. My current R experience is fairly limited. Any recommendations on how I could append the value of i into the variable name?

Apologies for not doing the MRE earlier.


   

 library(tidyverse)
        WA_Cases_Short <- structure(list(County = c("Adams County, Washington",
        "Asotin County, Washington", "Benton County, Washington"),
        `06/21/20` = c(108, 20, 1427), `06/22/20` = c(109, 20, 1455),
        `06/23/20` = c(109, 20, 1512), `06/24/20` = c(110, 20, 1545), 
        `06/25/20` = c(112, 20, 1570), `06/26/20` = c(113, 20, 1651)),
        class = c("spec_tbl_df", "tbl_df", "tbl", "data.frame"), 
        row.names = c(NA, -3L), spec = structure(list(cols = list(County =
        structure(list(), class = 
        c("collector_character", "collector")), `06/21/20` = structure(list(), 
        class = c("collector_double", "collector")), `06/22/20` = structure(list(), 
        class = c("collector_double", "collector")), `06/23/20` = structure(list(),
        class = c("collector_double", "collector")), `06/24/20` = structure(list(),
        class = c("collector_double", "collector")), `06/25/20` = structure(list(),
        class = c("collector_double", "collector")), `06/26/20` = structure(list(),
        class = c("collector_double", "collector"))), default = structure(list(),
        class = c("collector_guess", "collector")), skip = 1L), class = "col_spec"))
    
        i <- 2
        while (i < ncol(WA_Cases_Short)) {
        WA_Cases_Short_Differences <- WA_Cases_Short %>%
        mutate(d_daily = (WA_Cases_Short[(i + 1)] - WA_Cases_Short[i]))
        #print(i) test to ensure while works correctly
        i <- i + 1
        }

What I am trying to do is take 2 of the variables and record the difference as a new variable in the new data frame. This new variable I want to be named sequentially as d_daily_1 d_daily_2 and so on. Right now my while loop does not make a new variable but seems to overwrite the previous one. I know the loop is cycling as I can print the test line.

I don't know if this is useful, but here is the sessionInfo()

R version 4.0.5 (2021-03-31)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19041)

Matrix products: default

locale:
[1] LC_COLLATE=English_United States.1252  LC_CTYPE=English_United States.1252   
[3] LC_MONETARY=English_United States.1252 LC_NUMERIC=C                          
[5] LC_TIME=English_United States.1252    

attached base packages:
[1] stats4    grid      stats     graphics  grDevices utils     methods   base     

other attached packages:
 [1] curl_4.3.1        RCurl_1.98-1.3    lubridate_1.7.10  magrittr_2.0.1    forcats_0.5.1    
 [6] stringr_1.4.0     dplyr_1.0.6       purrr_0.3.4       readr_1.4.0       tidyr_1.1.3      
[11] tibble_3.1.2      ggplot2_3.3.4     tidyverse_1.3.1   rio_0.5.26        psych_2.1.3      
[16] party_1.3-7       strucchange_1.5-2 sandwich_3.0-1    zoo_1.8-9         modeltools_0.2-23
[21] mvtnorm_1.1-2     pacman_0.5.1     

loaded via a namespace (and not attached):
 [1] httr_1.4.2         jsonlite_1.7.2     splines_4.0.5      tmvnsim_1.0-2     
 [5] modelr_0.1.8       assertthat_0.2.1   coin_1.4-1         cellranger_1.1.0  
 [9] pillar_1.6.1       backports_1.2.1    lattice_0.20-41    glue_1.4.2        
[13] rvest_1.0.0        colorspace_2.0-1   Matrix_1.3-2       pkgconfig_2.0.3   
[17] broom_0.7.7        haven_2.4.1        scales_1.1.1       openxlsx_4.2.3    
[21] datasets_4.0.5     generics_0.1.0     ellipsis_0.3.2     TH.data_1.0-10    
[25] withr_2.4.2        cli_2.5.0          mnormt_2.0.2       survival_3.2-10   
[29] crayon_1.4.1       readxl_1.3.1       fs_1.5.0           fansi_0.5.0       
[33] nlme_3.1-152       MASS_7.3-53.1      xml2_1.3.2         foreign_0.8-81    
[37] tools_4.0.5        data.table_1.14.0  hms_1.1.0          lifecycle_1.0.0   
[41] matrixStats_0.59.0 multcomp_1.4-17    munsell_0.5.0      reprex_2.0.0      
[45] zip_2.2.0          compiler_4.0.5     tinytex_0.32       rlang_0.4.11      
[49] rstudioapi_0.13    bitops_1.0-7       gtable_0.3.0       codetools_0.2-18  
[53] DBI_1.1.1          R6_2.5.0           utf8_1.2.1         libcoin_1.0-8     
[57] stringi_1.5.3      parallel_4.0.5     Rcpp_1.0.6         vctrs_0.3.8       
[61] dbplyr_2.1.1       tidyselect_1.1.1   xfun_0.23        

If I am understanding your code correctly you want to subtract column 2 with column 1, column 3 with column 2 and so on. If so you can do this directly in R without the need of the loop.

df <- mtcars[1:5, 1:6]
df[paste0('diff', seq_along(df[-1]))] <- df[-1] - df[-ncol(df)]

df

#                   mpg cyl disp  hp drat    wt diff1 diff2 diff3   diff4  diff5
#Mazda RX4         21.0   6  160 110 3.90 2.620 -15.0   154   -50 -106.10 -1.280
#Mazda RX4 Wag     21.0   6  160 110 3.90 2.875 -15.0   154   -50 -106.10 -1.025
#Datsun 710        22.8   4  108  93 3.85 2.320 -18.8   104   -15  -89.15 -1.530
#Hornet 4 Drive    21.4   6  258 110 3.08 3.215 -15.4   252  -148 -106.92  0.135
#Hornet Sportabout 18.7   8  360 175 3.15 3.440 -10.7   352  -185 -171.85  0.290

so for 6 columns this will generate 5 new columns in the dataframe.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM