Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
172 views
in Technique[技术] by (71.8m points)

r - assign items from list of lists as column names for data frames

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
question from:https://stackoverflow.com/questions/66052717/assign-items-from-list-of-lists-as-column-names-for-data-frames

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...