I have a list of data frames that are being pulled in from an Excel file. The Excel file has funky formatting and the true column names are the first row of the data frame. The list contains 9 data frames that are not sequentially named and are based on the tab names of the Excel file.
Here is what I have thus far:
for(i in all_list){
tmp <- get(i)
colnames(tmp) <- unlist(get(i)[1,])
assign(i, tmp)
}
R presents me with an error of:
Error in get(i) : invalid first argument
Here is a sample of the structure of my list of data frames:
str(all_list)
List of 9
$ Retail :'data.frame': 306 obs. of 25 variables:
$ X__1 : chr [1:306] NA NA "VARIABLE" "VARIABLE" ...
$ X__2 : chr [1:306] "TIME PERIOD" NA "41640" "41671" ...
As you can see, the column names with in the first element of the list (Retail) have the "X__#" format. Is there a clear way to do this reformatting in one loop over this list? Thank you.
You can use lapply
iterate through each data.frame in list to set the column name from 1st row. Remove the first row before data.frame is returned. Example as:
ll <- list(df1,df2,df3,df4)
lapply(ll, function(x){
names(x) <- x[1,]
x[-1,]})
#[[1]] df1
# g x <-- 1st row has been set as column name.
#2 j z
#3 n p
#4 u o
#5 e b
Sample Data:
set.seed(1)
df1 <- data.frame(First = sample(letters, 5), Second = sample(letters, 5),
stringsAsFactors = FALSE)
df2 <- data.frame(First = sample(letters, 5), Second = sample(letters, 5),
stringsAsFactors = FALSE)
df3 <- data.frame(First = sample(letters, 5), Second = sample(letters, 5),
stringsAsFactors = FALSE)
df4 <- data.frame(First = sample(letters, 5), Second = sample(letters, 5),
stringsAsFactors = FALSE)
df1
# First Second
# 1 g x
# 2 j z
# 3 n p
# 4 u o
# 5 e b
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加