我有一個 .xyz 和 .csv 擴展名的資料框串列。我嘗試通過應用函式來迭代 .xyz (egapalachicola_mile_76.xyz) 擴展名和 .csv 檔案;
split_func
拆分并從檔案名中獲取唯一值,然后將其復制到新列中。但是,它給出了一個錯誤:“有 30 個警告(使用 warnings() 來查看它們)”。
一次只能處理一個資料幀
# Load needed packages
library(tibble)
library(plyr)
library(readr)
filez <- list.files('.', full.names = T, pattern = '*.xyz')
# create a function to assign values
split_func <- function(mylist, df){
split_first <- unlist(strsplit(mylist, '_mile_'))[2] #split the dataframe name,select a value
split_sec <- unlist(strsplit(split_first, '\\.'))[1]
conv_num <- as.numeric(split_sec) #turn the selcted value to a number (integer)
add_column(df, RM = conv_num) # create new column and add the number
}
#iterate the function over each csv file
# first read the exported data
filez_df <- list.files('.', full.names = T, pattern = '.csv') #import exported data
#apply split_func function to all files
for(i in 1:length(filez_df)){ # iterate through the length of the file
df_holder <- vector(mode = 'list', length = length(filez_df)) # create an empty list
df_holder[i] <- split_func(filez[i], read.csv(filez_df[i])) # apply the function
uj5u.com熱心網友回復:
# Get paths to all .csv files in working dir
csvs <- list.files(pattern = ".csv")
xyzs <- list.files(pattern = ".xyz")
# Empty list to hold the result of each iteration
all_files <- list()
for(i in 1:length(csvs)){
temp <- read.csv(csvs[i])
mile_num <- sub(pattern = ".*_(\\d{ }).xyz", replacement = "\\1", x = xyzs[i])
temp$mile <- mile_num
all_files[[i]] <- temp
}
# Convert list of dataframes to a single dataframe
do.call(rbind, all_files)
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/453430.html
上一篇:GroupBy-Python/Pandas中按類別分類的總小時數和小時數
下一篇:如何在資料框中附加列
