0

I have several data frames without variables names. They are all different. I have a list with set of column names that go with each data frame (similar names for list items as data frames that match). I want to assign/use the column names on the data frame. I tried doing something with loops and then lapply but I just cannot get it. Below is an example of what I am talking about. I apologize up front for the less than perfect coding and explanation here. I am sure the answer is something I should be able to do. Any help is greatly appreciated.

#Activate a package
library(rio)
##################################################
#Create data frames
dir.create("data/")
sec1<- data.frame(x1 = 1:5,                
              x2 =c("L","M","N","O","P"),
              x3 = c(4, 1, 9, 0, 1))
#########
sec2<- data.frame(x1 = c("A","B","C","D","E"),                
              x2 = 1:5,
              x3 = c(12, 10, 29, 0, 5))
#########
sec3<- data.frame(x1 = c(100, 21, 91, 53, 35),                
              x2 = 1:5,
              x3 = c("W","X","Y","Z","A"))

#Export into folder as csv file
export(sec1,"data/sec1.csv")
export(sec2,"data/sec2.csv")
export(sec3,"data/sec3.csv")
#Clear out these files for now
rm(sec1,sec2,sec3)
###############################################
#Create a list of columns names
sec1<-c("A","B","C")
sec2<-c("AA","BB","CC")
sec3<-c("AAA","BBB","CCC")
#####
listAA<-list(sec1,sec2,sec3);listAA
rm(sec1,sec2,sec3)
#I was hoping to use loop command or lapply to use the list names for the data frames
aynber
  • 22,380
  • 8
  • 50
  • 63
  • I see 3 data frames, written as CSVs, and 3 vectors of column names. I understand you want to replace the data frame column names with those in `listAA`. But I'm confused by the create data frames / write data frames / `rm()` data frames piece of code. Is that just to set up an example? Do you want those column names to be applied to the CSV files (the files get updated) or do you want to read those sample CSV files in, applying the column names in the process? Or something else? – Gregor Thomas Feb 04 '21 at 19:54
  • Yes, sorry for any confusion. I have the data frames saved as csv files in a folder. The rm() is not important - I just used it in creating the example. I want to use the listAA names for the data frame column names. It would be updating the data frames. Again sorry for the confusion. – Keith Wiley Feb 04 '21 at 20:59
  • By "updating the data frames", do you want to update them in R's environment for analysis, or update the CSV files on your hard drive (or both)? – Gregor Thomas Feb 05 '21 at 04:48
  • Update the csv files on my hard drive. I will then merge the data in with other data for analysis. Again, thank you for your help. – Keith Wiley Feb 05 '21 at 16:24

1 Answers1

0

If I understand correctly, you want to add column names to a list of data.frames. Your code is confusing because you remove the data frames and then use their names for the column names:

sec1 <- data.frame(x1 = 1:5,                
              x2 =c("L","M","N","O","P"),
              x3 = c(4, 1, 9, 0, 1))
sec2 <- data.frame(x1 = c("A","B","C","D","E"),                
              x2 = 1:5,
              x3 = c(12, 10, 29, 0, 5))
sec3 <- data.frame(x1 = c(100, 21, 91, 53, 35),                
              x2 = 1:5,
              x3 = c("W","X","Y","Z","A"))
listDTA <- list(sec1=sec1, sec2=sec2, sec3=sec3)
listAA <- list(sec1 = c("A","B","C"), 
            sec2 = c("AA","BB","CC"), 
            sec3 = c("AAA","BBB","CCC"))

Now use a loop to assign the column names in listDTA to the data frames in list listAA:

n <- length(listDTA)
for (i in seq_len(n)) {
    colnames(listDTA[[i]]) <- listAA[[i]]
}
listDTA
# $sec1
#   A B C
# 1 1 L 4
# 2 2 M 1
# 3 3 N 9
# 4 4 O 0
# 5 5 P 1

# $sec2
#   AA BB CC
# 1  A  1 12
# 2  B  2 10
# 3  C  3 29
# 4  D  4  0
# 5  E  5  5

# $sec3
#   AAA BBB CCC
# 1 100   1   W
# 2  21   2   X
# 3  91   3   Y
# 4  53   4   Z
# 5  35   5   A
dcarlson
  • 10,936
  • 2
  • 15
  • 18