how to select list of directory and read files from those directory in R

user3378320

I am trying to select some specific directories and read one specific file from each directory.

Here is an example of directories and files

|-out_original
|-----result_file
|-out_20percent_ds
|-----result_file
|-out_40percent_ds
|-----result_file
|-out_60percent_ds
|-----result_file
|-out_80percent_Ds
|-----result_file

Code

setwd("/home/data/proj/")
datatype = c("20","40","60","80","original")
filenames=as.vector(c(0))

for (i in 1:length(datatype))
{
    if(i <= 5){
        filenames[i]=paste0("out_",datatype[i],"percent_ds/")
    }

    else{   
        filenames[i]=paste0("out_",datatype[i])
    }
}

How can I save files in to variables from each directory?

Anders Ellern Bilgrau

Try using list.files. If the working directory is the root of the directory you have shown, then something link this will work:

my.files <- 
  list.files(path = "./", pattern = "result_file", 
             full.names = TRUE, recursive = TRUE)

This recursively searches for files with names containing the pattern "result_file" in the path given. The pattern can be any regular expression and only files which match this regular expression are returned.

You can then read your files using something like

ans <- lapply(my.files, read.table)

assuming your data is tabular.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

how to read files from a directory by using R

From Dev

how to read files from a directory by using R

From Dev

How to read files in sequence from a directory in OpenCV?

From Dev

How to read multiple files from a resource directory

From Dev

How to read multiple files from a resource directory

From Dev

how to read files sequentially from a directory in java?

From Dev

How to list all files from the resources directory

From Dev

How to list recently deleted files from a directory?

From Dev

How to delete all files, excluding those in a directory?

From Dev

R read all files in a directory

From Dev

Read the list of msi files from a directory and save them in the array

From Dev

select certain files from directory

From Dev

Select certain files from a directory

From Dev

How to read files in the proc directory?

From Dev

How to know if a user can read and list files of a directory

From Dev

How to get R to read in files from multiple subdirectories under one large directory?

From Dev

How to set colnames and rownames when you read bunch of files from a directory into R?

From Dev

How to get R to read in files from multiple subdirectories under one large directory?

From Dev

How to search a a directory tree in TCL for files that end in .so or .a and build a list of those directories

From Dev

How to get and read the csv files in same order from a directory in python

From Dev

How can I read files from directory and send as JSON to client?

From Dev

How to read files from every zip folder present in a directory

From Dev

how to read files from directory using for loop using php

From Dev

How to sync files in directory a from directory b?

From Dev

How to properly list files of a directory?

From Dev

How to get the list of files in a directory

From Dev

Programming a game in java. I need to read class files from a related directory, and return an object created from those

From Dev

Programming a game in java. I need to read class files from a related directory, and return an object created from those

From Dev

Read first file in a list of files within a directory

Related Related

  1. 1

    how to read files from a directory by using R

  2. 2

    how to read files from a directory by using R

  3. 3

    How to read files in sequence from a directory in OpenCV?

  4. 4

    How to read multiple files from a resource directory

  5. 5

    How to read multiple files from a resource directory

  6. 6

    how to read files sequentially from a directory in java?

  7. 7

    How to list all files from the resources directory

  8. 8

    How to list recently deleted files from a directory?

  9. 9

    How to delete all files, excluding those in a directory?

  10. 10

    R read all files in a directory

  11. 11

    Read the list of msi files from a directory and save them in the array

  12. 12

    select certain files from directory

  13. 13

    Select certain files from a directory

  14. 14

    How to read files in the proc directory?

  15. 15

    How to know if a user can read and list files of a directory

  16. 16

    How to get R to read in files from multiple subdirectories under one large directory?

  17. 17

    How to set colnames and rownames when you read bunch of files from a directory into R?

  18. 18

    How to get R to read in files from multiple subdirectories under one large directory?

  19. 19

    How to search a a directory tree in TCL for files that end in .so or .a and build a list of those directories

  20. 20

    How to get and read the csv files in same order from a directory in python

  21. 21

    How can I read files from directory and send as JSON to client?

  22. 22

    How to read files from every zip folder present in a directory

  23. 23

    how to read files from directory using for loop using php

  24. 24

    How to sync files in directory a from directory b?

  25. 25

    How to properly list files of a directory?

  26. 26

    How to get the list of files in a directory

  27. 27

    Programming a game in java. I need to read class files from a related directory, and return an object created from those

  28. 28

    Programming a game in java. I need to read class files from a related directory, and return an object created from those

  29. 29

    Read first file in a list of files within a directory

HotTag

Archive