How can I change the column names of a list to the first row of each data frame in a loop?

Franchise

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.

MKR

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]

編集
0

コメントを追加

0

関連記事

分類Dev

How can I convert an R data frame with a single column into a corpus for tm such that each row is taken as a document?

分類Dev

How can I row bind the unmatch data in the column of first table from the second table

分類Dev

How to reshape a single row data frame when column names denote information

分類Dev

How do I extract column names in list of data frames?

分類Dev

How can I subtract an array from a single data frame column?

分類Dev

How can I count how many times each of the names in a names list is repeated?

分類Dev

How do I subset a data frame by row names that do not meet a condition?

分類Dev

First highest value in each frame in a list and add into a new column

分類Dev

Loop across list of dataframes, moving column 1 to row names?

分類Dev

Spark Window - How to compare first row with nth row of a data frame?

分類Dev

How do I swap N values in one data frame column for any value of N and any n(row)?

分類Dev

How to get only column names of data frame that are starting with particular characters?

分類Dev

How can I apply a function to every row of a data frame in R when the function requires multiple inputs?

分類Dev

How to start import from first numeric column in each row

分類Dev

How to feed a list of unquoted column names into `lapply` (so that I can use it with a `dplyr` function)

分類Dev

How can I get the first 4 row values into one row in seperate columns for each visit_id record?

分類Dev

How can I subset a data frame based on dates, when my dates column is not the index in Python?

分類Dev

How can I reset pct change to NaN when another column value is different than previous row?

分類Dev

Convert a string to data frame, including column names

分類Dev

Get column names of a data frame based on values from a list in pandas python

分類Dev

How do I increment row in mysql at each iteration of a"for" loop?

分類Dev

How can I pass a list of names to list.select?

分類Dev

How can I write named list to files (with the list names) with purrr

分類Dev

How to take values from a list to a column of the data frame in R

分類Dev

How can I get column names from a table in Oracle?

分類Dev

Loop through each column and row, do stuff

分類Dev

Diff on each subset of a data frame column

分類Dev

Change color of complete row in data frame in pandas

分類Dev

Make a feature the first column in data frame

Related 関連記事

  1. 1

    How can I convert an R data frame with a single column into a corpus for tm such that each row is taken as a document?

  2. 2

    How can I row bind the unmatch data in the column of first table from the second table

  3. 3

    How to reshape a single row data frame when column names denote information

  4. 4

    How do I extract column names in list of data frames?

  5. 5

    How can I subtract an array from a single data frame column?

  6. 6

    How can I count how many times each of the names in a names list is repeated?

  7. 7

    How do I subset a data frame by row names that do not meet a condition?

  8. 8

    First highest value in each frame in a list and add into a new column

  9. 9

    Loop across list of dataframes, moving column 1 to row names?

  10. 10

    Spark Window - How to compare first row with nth row of a data frame?

  11. 11

    How do I swap N values in one data frame column for any value of N and any n(row)?

  12. 12

    How to get only column names of data frame that are starting with particular characters?

  13. 13

    How can I apply a function to every row of a data frame in R when the function requires multiple inputs?

  14. 14

    How to start import from first numeric column in each row

  15. 15

    How to feed a list of unquoted column names into `lapply` (so that I can use it with a `dplyr` function)

  16. 16

    How can I get the first 4 row values into one row in seperate columns for each visit_id record?

  17. 17

    How can I subset a data frame based on dates, when my dates column is not the index in Python?

  18. 18

    How can I reset pct change to NaN when another column value is different than previous row?

  19. 19

    Convert a string to data frame, including column names

  20. 20

    Get column names of a data frame based on values from a list in pandas python

  21. 21

    How do I increment row in mysql at each iteration of a"for" loop?

  22. 22

    How can I pass a list of names to list.select?

  23. 23

    How can I write named list to files (with the list names) with purrr

  24. 24

    How to take values from a list to a column of the data frame in R

  25. 25

    How can I get column names from a table in Oracle?

  26. 26

    Loop through each column and row, do stuff

  27. 27

    Diff on each subset of a data frame column

  28. 28

    Change color of complete row in data frame in pandas

  29. 29

    Make a feature the first column in data frame

ホットタグ

アーカイブ