[R] set the wd based on cdv file

arun smartpink111 at yahoo.com
Wed Jul 31 00:41:55 CEST 2013


Hi,
It is not clear which OS you are using.  Most probably, the "Directory" column would be "factor".
table1<- read.table(text="
ID Directory
1  /home/arunksa111/Documents
2  /home/arunksa111/Trial
3  ~/Trial1
4 ~Trial2
",sep="",header=TRUE,stringsAsFactors=FALSE)

setwd(table1$Directory[2])
 getwd()
#[1] "/home/arunksa111/Trial"

setwd(table1$Directory[1])
getwd()
#[1] "/home/arunksa111/Documents"
T2<- subset(table1,ID==3)
setwd(T2$Directory)
 getwd()
#[1] "/home/arunksa111/Trial1"
 T3<-subset(table1,ID==4)
setwd(T3$Directory)
#Error in setwd(T3$Directory) : cannot change working directory
#The error is different here.

#Now using stringsAsFactors=TRUE

table1<- read.table(text="
ID Directory
1  /home/arunksa111/Documents
2  /home/arunksa111/Trial
3  ~/Trial1
4 ~Trial2
",sep="",header=TRUE,stringsAsFactors=TRUE)

 setwd(table1$Directory[2])
#Error in setwd(table1$Directory[2]) : character argument expected
T2<- subset(table1,ID==3)
 setwd(T2$Directory)
#Error in setwd(T2$Directory) : character argument expected
 T3<-subset(table1,ID==4)
 setwd(T3$Directory)
#Error in setwd(T3$Directory) : character argument expected
setwd(as.character(T3$Directory)) #path is  not correct
#Error in setwd(as.character(T3$Directory)) : 
 # cannot change working directory
 setwd(as.character(T2$Directory))
 getwd()
#[1] "/home/arunksa111/Trial1"

sessionInfo()
R version 3.0.1 (2013-05-16)
Platform: x86_64-unknown-linux-gnu (64-bit)

A.K.



Hello, 

I have a CSV file that summarises different working directories.
 I would like to write a script to be able to access the different 
directories. 

CSV "table" file: 
ID              directory 
1                "~Documents/16_06_13" 
2                "~Documents/18_06_13" 
3                "~Documents/19_06_13" 
... 

I tried by sub setting the "table.csv" file: 
T1 <- subset(table, table$ID== "1") 
setwd(T1$directory) 

but it gives me the following error: 
Error in setwd(T1$directory) : character argument expected 

can someone help me to solve this problem? 

Thank you very much 

Stefano



More information about the R-help mailing list