0

I need to recall the variable within the loop and the use that as my column name:

This is an example dataset:

mtcars.df <- mtcars

Expected output:

mtcars.df <- mtcars.df %>% add_column(a1 = sample (1:4, 32, replace = TRUE), b1 = sample (1:4, 32, replace = TRUE), c1 = sample (1:4, 32, replace = TRUE))

So a1, b1 and c1 need to be created within the loop and renamed to a1, b1 and c1. The names are longer (and different combinations) in the original dataset but this is defined in variable mpg.filename.

This is what I have tried so far: The 1st obviously doesn't give the desired result but it's just to show what I want to achieve.

mpg.filename <- c("a1.file", "b1.file", "c1.file")
for (i in mpg.filename) {
  sample.name <- unlist(strsplit(as.character(i), '.', fixed = TRUE))[1]
  mtcars.df$i <- sample (1:4, 32, replace = TRUE)
}

for (i in mpg.filename) {
  sample.name <- unlist(strsplit(as.character(i), '.', fixed = TRUE))[1]
  mtcars.df$temp.var <- sample (1:4, 32, replace = TRUE)
  temp.name <- paste0 (sample.name) %>% rlang::parse_expr()
  mtcars.df <- mtcars.df %>% rename (eval (sample.name) = temp.var)
}

for (i in mpg.filename) {
  sample.name <- unlist(strsplit(as.character(i), '.', fixed = TRUE))[1]
  mtcars.df$temp.var <- sample (1:4, 32, replace = TRUE)
  temp.name <- paste0 (sample.name) %>% rlang::parse_expr()
  mtcars.df <- mtcars.df %>% rename (syms(sample.name) = temp.var)
}

I have tried get, as.symbol, parse (text = "sample.name") as well but didn't work either.

Thanks for the help. I have tried looking at other answers on forums but they do not seem to apply or work.

using eval() on string to access object attributes in R

call columns from inside a for loop in R

Getting strings recognized as variable names in R

How to evaluate an expression with variables in R?

2 Answers 2

1

Your first attempt was close! When you want to access / create columns in a data.frame using a character, you need to use [[ instead of $. No need for symbols / parsing / other complicated nonsense if this is all you need to do.

for(i in mpg.filename) {
  sample.name <- unlist(strsplit(as.character(i), '.', fixed = TRUE))[1]
  mtcars.df[[sample.name]] <- sample(1:4, 32, replace=TRUE)
}
Sign up to request clarification or add additional context in comments.

Comments

0

Let me know if this did the trick-

mpg.filename <- c("a1.file", "b1.file", "c1.file")
for (i in 1:length(mpg.filename)) {
  sample.name <- unlist(strsplit(as.character(mpg.filename[i]), '.', fixed = TRUE))[1]
  mtcars.df$i <- sample (1:4, 32, replace = TRUE)
  colnames(mtcars.df)[length(mtcars.df)]<-paste(sample.name)
}

2 Comments

My original code and variable mpg.filename is imported as list.files () and then I am reading in a file that is determined by i. I did not indicate that in the original question. My apologies. Your answer does work for but when mpg.filename is imported as list.files () and then I try to read it as a file within the loop it throws an error because i now is a integer and cannot read the file. It is a good solution though.
Not sure if I understand exactly, but to go from i to the item in your list, pull it out with: mpg.filename[i]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.