Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using file.exist in R

Tags:

r

I'm new to R and i have question about using file.exist.

I tried:

if(!file.exist("data")){
    dir.create("data")
}

But I get the error, could not find function "file.exist".

I then tried:

if (is!TRUE(file.exists("data"))) {
     dir.create("data")
}

I still get an error, unexpected '!' in "if (is!". But it creates the folder.

What am I doing wrong?

like image 930
hIlary Avatar asked Sep 13 '18 17:09

hIlary


2 Answers

Your are looking for the following:

if(!dir.exists("data")) {
    dir.create("data")
}

Here are some Links that might help you on the way:

Boolean Operators

files2 package for file system interfacing

like image 94
darian_ Avatar answered Oct 23 '22 21:10

darian_


While this is potentially a duplicate, I think it's worth a little explanation for you.

if(!file.exists("data")){
    dir.create("data")
}

This is the right way to go about it, you've done that well. Your issue is R doesn't know where "data" lives if you've not set the working directory to the location that data would or wouldn't exist. 2 ways to tackle this: 1:

setwd("C:/folder/folder/folder/data_location")
if(!file.exists("data")){
    dir.create("data")
}

2:

if(!file.exists("C:/folder/folder/folder/data_location/data")){
   dir.create("data")
}

Something else I've noticed is you're looking for a file, then creating a directory. If you're interested in a directory, check out dir.exists.

Hope this helps!

like image 34
Badger Avatar answered Oct 23 '22 22:10

Badger