Home > database >  Can I add column names to a variable while I run a `for` loop in R?
Can I add column names to a variable while I run a `for` loop in R?

Time:10-03

I have an exercise that I'm doing in R that requires me to find the stem and leaf plot for a few variables. For example the first iteration of this process would be:

> with(data = Commercial_Properties, stem(x = Op_Expense_Tax))

  The decimal point is at the |

   2 | 0
   4 | 080003358
   6 | 012613
   8 | 00001223456001555689
  10 | 013344566677778123344666668
  12 | 00011115777889002
  14 | 6

I would have to do this repeatedly for a few more variables after this. So in my path towards improvement I recall a friend of mine who is well versed in programming mentioning that if you are doing the same task over repeatedly then it calls for a for loop of some sort to be done.

As a result I attempted do as such:

for (i in 2:5){
  
  stem_colnames(Commercial_Properties[i]) = with(data = Commercial_Properties, stem(x = unlist(Commercial_Properties[,i])))
  
}

What I wanted the code to do was extract the column name from my data frame, append it to the stem_ to create the name of the respective variable and also then produce the respective stem and leaf plot. I could most likely do this manually but I was wondering if it is possible to automate the process? Am I being too ambitious in hoping I could name my variables iteratively as well?

To reproduce the example the following is the dput output.

 dput(head(Commercial_Properties, 5))
structure(list(Rental_Rates = c(13.5, 12, 10.5, 15, 14), Age = c(1, 
14, 16, 4, 11), Op_Expense_Tax = c(5.02, 8.19, 3, 10.7, 8.97), 
    Vacancy_Rate = c(0.14, 0.27, 0, 0.05, 0.07), Total_Sq_Ft = c(123000, 
    104079, 39998, 57112, 60000)), row.names = c(NA, -5L), class = c("tbl_df", 
"tbl", "data.frame"))

EDIT: packages used: tidyverse, car

CodePudding user response:

Consider using cat

for (i in 2:5){cat(names(Commercial_Properties)[i], "\n")
  stem(Commercial_Properties[[i]])
}

-output

Age 

  The decimal point is 1 digit(s) to the right of the |

  0 | 14
  0 | 
  1 | 14
  1 | 6

Op_Expense_Tax 

  The decimal point is at the |

   2 | 0
   4 | 0
   6 | 
   8 | 20
  10 | 7

Vacancy_Rate 

  The decimal point is 1 digit(s) to the left of the |

  0 | 057
  1 | 4
  2 | 7

Total_Sq_Ft 

  The decimal point is 4 digit(s) to the right of the |

   2 | 
   4 | 07
   6 | 0
   8 | 
  10 | 4
  12 | 3

Or if we need a function

f1 <- function(dat, colind) {
   for(i in colind) {
        cat(names(dat)[i], "\n")
        stem(dat[[i]])
   }
   }
f1(Commercial_Properties, 2:5)

Or this can be done with iwalk

library(purrr)
iwalk(Commercial_Properties, ~ {cat(.y, "\n"); stem(.x)})
  • Related