将文件夹中的csv文件导入R中

时间:2017-03-16 18:20:21

标签: r csv

我正在尝试在R中导入一个包含多个.csv文件的文件夹,并读取所有文件的第二和第三列。我使用了以下命令,但它抛出一个错误,说“文件中的错误(文件,”rt“):无效的'description'参数 代码在这里

public static void main(String[] args)
{

    Scanner keyboard = new Scanner(System.in);

    System.out.println("[-------------------------]");
    System.out.println("[      Array Pattern      ]");
    System.out.println("[-------------------------]");

    System.out.println("How many rows/columns do you want your array to have? (Mist be at least 3):");


    int arraySize = keyboard.nextInt();

    while(arraySize < 3)
    {
        System.out.println("Lets try this again ....");
        System.out.println("How many rows/columns do you want your array to have? (Mist be at least 3):");
        arraySize = keyboard.nextInt();
    }


    int [][] pattern = new int[arraySize][arraySize];

    int number = 1;

    for(int i=0;i<arraySize;i++){
        if(i%2==0){
          for(int j=0;j<arraySize;j++){
               pattern[i][j]=number;
                number++;
             }
        }
        else{
            for(int j=arraySize-1;j>=0;j--){
                pattern[i][j]=number;
                number++;
            }
        }
        for(int j=0;j<arraySize;j++){
            System.out.printf("%3d", pattern[i][j]);
        }
        System.out.println();
    }
}

1 个答案:

答案 0 :(得分:0)

如果您的csv文件都具有相同的列标题,那么我认为这将适用于您要完成的任务:

folder <-"C:\\\\Users\\USER\\Desktop\\LABWORK\\ECGV0007_everyRRQTinputIntoEntropy_csv" 
file_list - list.files(path = folder, pattern = "*.csv", full.names = T)

dfs <- list()

for(i in 1:length(file_list)) {

    # read in the ith csv
    temp_df <- read.csv(file = file_list[[i]], stringsAsFactors = F)

    # store in a list
    dfs[[i]] <- temp_df
}


# combine all data frames in the list into a single data frame
combined_df <- do.call(rbind, dfs)


# extract 2nd col into x and 3rd col into y
x <- combined_df[, 2]
y <- combined_df[, 3]

编辑:

full.names = T添加到file_list对象,这样您就不必将文件夹和文件名粘贴在一起。