Passing string to file name in R [duplicate] - r

This question already has answers here:
Adding string to variable name
(2 answers)
Closed 9 years ago.
I have a variable a=0.01
Then I create a matrix: b<-matrix(data=1:5,ncol=5,nrow=1)
I would now like to save the matrix so that the name of the matrix is the value stored in a:
save(b_'string', file="b_'string'.Rdata")
Where 'string' should be the value stored in a, i.e. 0.01
So the file should be called b_0.01.Rdata and the variable stored should be b_0.01

You need to create a new string to feed to file, e.g. using paste0:
save(b,file=paste0("b_",a,".Rdata"))

Related

Why is there a space between strings when using the 'paste' function in R? I have used a separator [duplicate]

This question already has answers here:
What are the differences between "=" and "<-" assignment operators?
(9 answers)
What's the difference between `=` and `<-` in R? [duplicate]
(2 answers)
Closed 2 years ago.
When I input the following paste command:
rdsfile<-paste("Eobs","tmean","_81_10.rds",sep="")
I get: "Eobstmean_81_10.rds"
When I change the equals sign to the arrow usually used in R:
rdsfile<-paste("Eobs","tmean","_81_10.rds",sep<-"")
I get: "Eobs tmean _81_10.rds " (Note the space between "Eobs" and "tmean" and again between "tmean" and "_81_10.rds"
What is causing this to happen?
Parameters in functions should be assigned with an equals sign as in your first example so that you get your desired output.
What you are doing in your second example is to create a global object called sep with value "". You can easily check this by looking into your environment. Or simply type sep into the console.
But this global object sep is not used in the paste function, so the paste function falls back to its default, which is using a space (" ") as the separator.
Long story short: don't use <- for parameter assignments in functions.

How to check if data frame exists in R environment? [duplicate]

This question already has an answer here:
Verify object existence inside a function in R [duplicate]
(1 answer)
Closed 5 years ago.
I am trying to add rows to a data frame if it exists, or assign it to initial data frame in case it doesn't exists. I have tried exists(), missing(), etc, but nothing is working for me.
exists(data) && is.data.frame(get(data))
Error in exists(data) : object 'data' not found
Any help would be highly appreciated.
I am trying to do something like
if(exists(data))
data <- rbind(data,new_data)
else
data <- new_data
If you read the documentation you’ll see that it says that exists requires
a variable name (given as a character string).
In other words, write:
exists('data') && is.data.frame(get('data'))

Naming output files in R [duplicate]

This question already has answers here:
Concatenate a vector of strings/character
(8 answers)
Closed 6 years ago.
I'm working in R and I would like to export a txt file putting in its name the value of a particular variable; I read about the command paste and it works perfectly here:
write.table(mydata,file=paste(cn,"data.txt"))
where cn is the value to put at the beginning of the file data.txt. I would like to automatically put this file in an output folder where I keep all the other results. I try to do something like this:
write.table(mydata,file=paste(cn,"./output/data.txt"))
But it doesn't work. Any suggestion?
paste() just creates a string by concatenating the individual values and uses a space as default separator:
write.table(mydata, file = paste("./output/", cn ,"data.txt", sep = ""))
or with paste0(...), which is equivalent to paste(..., sep = ""):
write.table(mydata, file = paste0("./output/", cn ,"data.txt"))

Accessing dynamically named tables using a new object in R [duplicate]

This question already has an answer here:
How to call an object with the character variable of the same name
(1 answer)
Closed 7 years ago.
I have some sequentially labeled data frames i.e frame_1 frame_2 e.t.c... I would like to access them in a sequential manner possibly using a loop
one way that makes sense to me is to assign the name of the data frame I want to access to an object, then pass that object to a function i.e
varname<-paste("frame_",1,_sep="")
then call my function
function(varname)
But R appears to call the function on a string varname, and not the object with the same name as varname.
Is there way I can do what I want?
Thanks.
I found out you can parse a string as an R command using a combination of eval and parse, so for instance :
function( eval( parse(text=paste0("name_",1))) )
In a loop:
for( i in 1:length(holder)){
function(eval( parse(text=paste0("frame_",i))) )
}

File picking using pattern in R [duplicate]

This question already has answers here:
R-project filepath from concatenation
(1 answer)
Passing directory path as parameter in R
(1 answer)
Closed 8 years ago.
I have a directory which is having multiple files which starts with 001.csv, 002.csv and so on. I want to pick those files in a function for which I pass as an argument to the function.
For ex.
myFiles<-function(x=1:30){
// I should pick only those files which starts with 001.csv till 030.csv.
}
I tried using pattern matching but I am not sure how to make pattern matching using another variable which consists of vectors. I even tried using paste function so as to paste the full file path but it was giving me file name as 1.csv and not 001.csv
tt<-function(dirname,type,nums=1:30){
filenames<-list.files(dirname)
c<-nums
myVector<-0
for(i in 1:length(c)){
myVector[i]<-paste(dirname,"/",c[i],".csv",sep="")
#print(myVector[i])
}
}
One way you are able to get the correct names is to pad the start of the numbers with 0s using formatC e.g.
paste0(formatC(seq(1:30), width = 3, format = "d", flag = "0"), ".csv")

Resources