0

enter image description hereenter image description hereI continue to run into invalid 'path' argument when running my new script. I have a list of States in a .csv I have import and I am writing a for loop to dir.create.

I have tried Multiple ways to write this but not working. I can successfully create a dir by simply writing dir.create("new"), however it does not create when I use my variable in a for loop.

library(rvest)
library(dplyr)
library(tm)
library(stringi)
library(readr)

states = read.csv('C:/Users/mike/Desktop/Housing_Data/states/stateslist.csv', header = TRUE)

for(state in states){
  print(as.character(state))
  setwd('C:/Users/mike/Desktop/Housing_Data/states/')
  dir.create(paste0('C:/Users/mike/Desktop/Housing_Data/states/', state))
}

I expected the new directories to be created but running into path error

10
  • just dir.create(state) should be enough. And setwd() is not necessary inside the loop Commented Sep 8, 2019 at 16:01
  • tried that as well. I removed the setwd() as you recommended Commented Sep 8, 2019 at 16:06
  • would you please post the output of states Commented Sep 8, 2019 at 16:19
  • It is in a date frame named states: Header: states,Alabama,Alaska, etc. 50 obs. of 1 variable. Do I need to convert them to a character or string? not to sure Commented Sep 8, 2019 at 16:22
  • Please add the complete output to the original post. It's hard to debug without seeing exactly what you're giving as input for the path parameter. Commented Sep 8, 2019 at 16:24

1 Answer 1

1

You need to loop over the column of your data frame in my example states$state, in yours data frame it should be states$states. This works for me:

states <- data.frame(state = c("foo", "bar"))

setwd('C:/Users/mike/Desktop/Housing_Data/states/')
for(state in states$state){
  print(as.character(state))

  dir.create(state)
}

alternative: without setwd()

states <- data.frame(state = c("foo", "bar"))

for(state in states$state){

  print(as.character(state))

  dir.create(paste0('C:/Users/mike/Desktop/Housing_Data/states/', state))
}
Sign up to request clarification or add additional context in comments.

1 Comment

Got it. Adding the $state into the for loop was the trick. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.