I'm using the following code in R to read several xlsx files: loop through each file for…
- User
I'm using the following code in R to read several xlsx files:
loop through each file
for (file_name in file_names){
code# read in the xlsx file sheet_names <- excel_sheets(file_name) sheet_data <- lapply(sheet_names, function(sheet_name){ read_excel(file_name, sheet = sheet_name, col_names = T) }) # filter to only the “X Data” sheet tryCatch({ x_data <- sheet_data[sheet_names == "X Data"][[1]] }, error = function(e){ message(paste0("Sheet not found in file: ", file_name)) return(NULL) }) # select only the desired columns if (!is.null(x_data)) { selected_cols <- c("Batch No", "Day 0 Date")But the problem I'm having is that my "Day 0 Date" is being read in as text but it is actually a date column in my xlsx
- Assistant
file. To fix this, you can add the "col_types" argument to your read_excel function call and specify that the column "Day 0 Date" should be read in as a date column.
Replace this line of code:
read_excel(file_name, sheet = sheet_name, col_names = T)
With this:
read_excel(file_name, sheet = sheet_name, col_names = T, col_types = c("text", "date"))
This specifies that the first column should be read in as text and the second column ("Day 0 Date") should be read in as a date column. You can adjust the order of the col_types vector if your columns are in a different order than in this example.
- User
The problem with that is this:
Error: Sheet 1 has 97 columns, but
col_typeshas length 2. - Assistant
I see, thanks for letting me know. In that case, you may need to specify the column type for each column in the sheet. You can use the
col_typesparameter to achieve this.Here's an example of how you can specify the columns types when reading in the "X Data" sheet:
code# Read in the xlsx file sheet_names <- excel_sheets(file_name) sheet_data <- lapply(sheet_names, function(sheet_name){ read_excel(file_name, sheet = sheet_name, col_names = T, col_types = c("text", "date", "text", "numeric", rep("text", 92))) }) # Filter to only the “X Data” sheet tryCatch({ x_data <- sheet_data[sheet_names == "X Data"][[1]] }, error = function(e){ message(paste0("Sheet not found in file: ", file_name)) return(NULL) })In the example above, the
col_typesparameter specifies that the first column should be read in as "text", the second column (Day 0 Date) should be read in as "date", the third column as "text", and the 4th column as "numeric". The next 92 columns are assumed to be of type "text" so the is used to repeat the "text" type for those columns. - User
Is there a way to convert Day 0 Date to a date format after it is read in as text
- Assistant
Yes, you can convert the "Day 0 Date" column to a date format after reading it in as text. Here's an example of how you can do this using the
lubridatepackage:code# Load the library for manipulating dates library(lubridate) # Read in the xlsx file sheet_names <- excel_sheets(file_name) sheet_data <- lapply(sheet_names, function(sheet_name){ read_excel(file_name, sheet = sheet_name, col_names = T) }) # Filter to only the “X Data” sheet tryCatch({ x_data <- sheet_data[sheet_names == "X Data"][[1]] }, error = function(e){ message(paste0("Sheet not found in file: ", file_name)) return(NULL) }) # Convert "Day 0 Date" column to a date format if (!is.null(x_data)) { # Select the "Day 0 Date" column day_0_dates <- x_data[["Day 0 Date"]] # Convert the column to a date format using lubridate day_0_dates <- lubridate::mdy(day_0_dates) # Replace the "Day 0 Date" column in x_data with the converted dates x_data <- dplyr::mutate(x_data, "Day 0 Date" = day_0_dates) } - User
Right now I'm using this code:
selected_cols <- c("Batch No", "Day 0 Date", "FP ENDO \r\nEndotoxin Concentration [EU/ml]")
Some of the columns in my various xlsx files are not named exactly like "FP ENDO \r\nEndotoxin Concentration [EU/ml]". Is there any way to specify the column by if it contains "FP ENDO"
- Assistant
Yes, you can use the
greplfunction to select columns based on whether they contain a particular string. Here's an example of how you can select columns that contain the string "FP ENDO" in the name:code# Read in the xlsx file sheet_names <- excel_sheets(file_name) sheet_data <- lapply(sheet_names, function(sheet_name){ read_excel(file_name, sheet = sheet_name, col_names = T) }) # Filter to only the “X Data” sheet tryCatch({ x_data <- sheet_data[sheet_names == "X Data"][[1]] }, error = function(e){ message(paste0("Sheet not found in file: ", file_name)) return(NULL) }) # Select columns that contain "FP ENDO" in the name selected_cols <- names(x_data)[grepl("FP ENDO", names(x_data))] # Add other desired columns selected_cols <- c(selected_cols, "Batch No", "Day 0 Date")