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