I'm new to R and have a problem
I am trying to reformat some data, and in the process I would like to rename the columns of the new data set.
here is how I have tried to do this:
first the .csv file is read in, lets say case1_case2.csv then the name of the .csv file is broken up into two parts each part is assigned to a vector so it ends up being like this:
xName=case1 yName=case2 After I have put my data into new columns I would like to rename each column to be case1 and case2
to do this I tried using the rename function in R but instead of renaming to case1 and case2 the columns get renamed to xName and yName.
here is my code:
for ( n in 1:length(dirNames) ){ inFile <- read.csv(dirNames[n], header=TRUE, fileEncoding="UTF-8-BOM") xName <- sub("_.*","",dirNames[n]) yName <- sub(".*[_]([^.]+)[.].*", "\\1", dirNames[n]) xValues <- inFile %>% select(which(str_detect(names(inFile), xName))) %>% stack() %>% rename( xName = values ) %>% subset( select = xName) yValues <- inFile %>% select(which(!str_detect(names(inFile), xName))) %>% stack() %>% rename(yName = values, Organisms=ind) finalForm <- cbind(xValues, yValues) %>% filter(complete.cases(.)) } how can I make sure that the variables xName and yName are expanded inside of the rename() function
thanks.