Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R equivalent of Stata local or global macros

I am a Stata user trying to learn R.

I have a couple of lengthy folder paths which, in my Stata code, I stored as local macros. I have multiple files in both those folders to use in my analysis.

I know, in R, I can change the working directory each time I want to refer to a file in one of the folders but it is definitely not a good way to do it. Even if I store the folder paths as strings in R, I can't figure out how to refer to those. For example, in Stata I would use `folder1'.

I am wondering if trying to re-write Stata code line by line in R is not the best way to learn R.

Can someone please help?

like image 213
user2012406 Avatar asked Mar 28 '13 20:03

user2012406


1 Answers

Maybe you want file.path()?

a <- "c:"
b <- "users"
c <- "charles"
d <- "desktop"

setwd(file.path(a,b,c,d))
getwd()
#----
[1] "c:/users/charles/desktop"

You can wrap source or read.XXX or whatever else around that to do what you want.

like image 124
Chase Avatar answered Sep 30 '22 18:09

Chase