[R] Where the data file is stored?

Stephen Liu satimis at yahoo.com
Thu Aug 12 17:51:39 CEST 2010


Hi Barry,


Following 2 commands are useful to me;
> row.names(subset(file.info(list.files(getwd(),full.name=TRUE)),isdir))
showing directories.

> row.names(subset(file.info(list.files(getwd(),full.name=TRUE)),!isdir))
showing files


What is "!" for?  TIA


B.R.
Stephen L




----- Original Message ----
From: Barry Rowlingson <b.rowlingson at lancaster.ac.uk>
To: Stephen Liu <satimis at yahoo.com>
Cc: r-help at r-project.org
Sent: Thu, August 12, 2010 8:05:10 PM
Subject: Re: [R] Where the data file is stored?

On Thu, Aug 12, 2010 at 12:37 PM, Stephen Liu <satimis at yahoo.com> wrote:
> ----- Original Message ----
>
> From: Alain Guillet <alain.guillet at uclouvain.be>
> To: Stephen Liu <satimis at yahoo.com>
> Cc: r-help at r-project.org
> Sent: Thu, August 12, 2010 5:28:32 PM
> Subject: Re: [R] Where the data file is stored?
>
>> You can find your current working directory with the getwd() function.
>
>
> Hi Alain,
>
>
> Thanks for your advice.
>
>
>> getwd()
> [1] "/home/userA"
>
>
>> list.files(getwd())
>  [1] "Desktop"           "Documents"         "Downloads"
>  [4] "examples.desktop"  "last-packages.txt" "Music"
>  [7] "myR"               "petdrug.csv"       "Pictures"
> [10] "Public"            "R"                 "Templates"
> [13] "Videos"
>
>
>> list.files(getwd(),full.name=TRUE)
>  [1] "/home/userA/Desktop"
>  [2] "/home/userA/Documents"
>  [3] "/home/userA/Downloads"
>  [4] "/home/userA/examples.desktop"
>  [5] "/home/userA/last-packages.txt"
>  [6] "/home/userA/Music"
>  [7] "/home/userA/myR"
>  [8] "/home/userA/petdrug.csv"
>  [9] "/home/userA/Pictures"
> [10] "/home/userA/Public"
> [11] "/home/userA/R"
> [12] "/home/userA/Templates"
> [13] "/home/userA/Videos"
>
>
> How to show on the printout which is directory?  TIA

Use file.info and check the $isdir part of the returned data frame.
For example, to get names of only directories in your working dir, do:

row.names(subset(file.info(list.files(getwd(),full.name=TRUE)),isdir))

a quick modification gets you not-directories (which will be plain
files plus special files):

row.names(subset(file.info(list.files(getwd(),full.name=TRUE)),!isdir))

see ?file.info and ?files

Barry






More information about the R-help mailing list