0

I am trying to write a script that performs the following sequence to a variable containing a list of csv files:

-reads each csv file

-appends a column to each csv with the header "Dimension Type" where each row in the new column contains the name of the header in the first column

-renames the header of the first column to "Dimension"

-combines all of the csv files

-exports the combined csv files to an excel file

All of the csv files are structured the same with the exception of the header of the first column, which is the name of the dimension type

I created the variable containing the list of csv files using the list.files function and I have the directory where I want to store the excel file stored in a variable as well.

3
  • I had to use lapply with read.csv on my list of csv's to get the function to work properly. Otherwise, your function worked perfectly. Thank you! Commented Dec 10, 2021 at 3:13
  • Next time, you should post the code that you tried in the question. stackoverflow.com/questions/5963269/… Commented Dec 16, 2021 at 4:52
  • Please provide enough code so others can better understand or reproduce the problem. Commented Dec 16, 2021 at 4:53

1 Answer 1

0

Would something like this work? since I dont have your data I just made some quick datasets. (edited to include using it with lists)

df1 <- data.frame(dtype1 = sample(letters, 25, replace = TRUE),
                 col1 = sample(1:10, 25, replace = TRUE),
                 col2 = sample(5000:10000, 25))

df2 <- data.frame(dtype2 = sample(letters, 25, replace = TRUE),
                  col1 = sample(1:10, 25, replace = TRUE),
                  col2 = sample(5000:10000, 25))

df.list <- list(df1,df2)

SO_function <- function(df){
  df$`Dimension Type` <- colnames(df)[1]
  colnames(df)[1] <- "Dimension"
  return(df)
}

# apply to list
df.list_want <- lapply(df.list, SO_function)

# or apply to individual df's
df1.want <- SO_function(df1)
df2.want <- SO_function(df2)


#> head(df1.want, 5)
#  Dimension col1 col2 Dimension Type
#1         t    7 5921         dtype1
#2         r    4 5772         dtype1
#3         d    1 6722         dtype1
#4         u    2 8750         dtype1
#5         f    2 9778         dtype1
#> head(df2.want, 5)

#  Dimension col1 col2 Dimension Type
#1         k    5 5882         dtype2
#2         c    8 6237         dtype2
#3         f    6 6484         dtype2
#4         d    4 8504         dtype2
#5         i    7 7022         dtype2
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.