How to save the R output in different directory? - r

I have three different folders name as "5k", "10k" and "15k", I can save the R out from the following code using this for loop.
iter_no=c(5000,10000,15000)
iter_name=c("5k","10k","15k")
for ( i in 1:length(iter_no)){
y=rnorm(iter_no[i])
setwd(paste0("C:/Users/Owner/Desktop/prac_fol/",iter_name[i]))
save(y, file =paste0("ydat",iter_name[i],".RData"))
}
Is there any shortcut or better way to do this.
Any help is appreciated.

Try the following code. It looks like you are omitting / in the second call to paste0.
iter_no=c(5000,10000,15000)
iter_name=c("5k","10k","15k")
for ( i in 1:length(iter_no)){
y=rnorm(iter_no[i])
file = paste0("C:/Users/Owner/Desktop/prac_fol/",iter_name[i], '/' , "ydat",iter_name[i],".RData")
save(y, file = file)
}

Related

Running multiple files from with Scilab program

I'm new to Scilab. I have to run the same program with a dozen different input files. Currently I simply uncomment the line and then rerun the program, and change the output file to a new name
// Input data file
data_file = 'data1.txt';
//data_file = 'data2.txt';
//data_file = 'data3.txt';
//data_file = 'data4.txt';
//data_file = 'data5.txt';
//data_file = 'data6.txt';
etc. another 6 lines
// Output data file name
output_data = '/output_files/data1.csv';
Is there a way to read in each file (data1.txt, data2.txt...) execute the body of the program and then output a new output file (data1.csv, data2.csv ...) instead of what I'm doing now, which is running the program and then editing it to use the next file and run again?
Just do something like:
for i=1:6
// Input data file
data_file=msprintf("data%d.txt",i);
// Output data file name
output_data=msprintf("/output_files/data%d.csv",i);
// exec the body of your script
end

R function to write results in to a csv file

Hi I have created this R function two do some operation and store the results into separate files.
x1=rnorm(100,0,1)
x2=rnorm(100,0,1)
dataaa=data.frame(x1,x2)
func1=function(dataaa,name1,name2)
{
xqr=dataaa[,1]^2
xcube=dataaa[,1]^3
write.csv(xqr,"name1.csv")
write.csv(xcube,"name2.csv")
}
func1(dataaa,xr,xc)
The function works well. But the file name didnt change. i.e the names of the two csv files should be xr.csv and xc.csv. But it was saved as name1.csv and name2.csv.
How to modify this function so that I can get the correct file names?
Thank you.
this should work:
func1=function(dataaa,name1,name2)
{
xqr=dataaa[,1]^2
xcube=dataaa[,1]^3
write.csv(xqr, paste0(name1,".csv"))
write.csv(xcube, paste0(name2,".csv"))
}

Converting the argument name of a function into string

I have developed a function which will take a list of files and will do some statistical tests and will generate a excel file. In the last line of function (return object) I want the function will return a excel file with same names as input file names. In my example it will give list_file.xlsx. IF I enter another file let's say tslist_file it should automatically return tslist_file.xlsx. The function is properly working. Suggest me how I code last line of the function so that I can generalise it.
newey<-function(list_files){
tsmom<-do.call(cbind,lapply(list_files,function(x) read_excel(x)[,2]))
tsmom<-xts(tsmom[,1:5],order.by = seq(as.Date("2005-02-01"),length=183,by="months")-1)
names(tsmom)<-c("tsmom121","tsmom123","tsmom126","tsmom129","tsmom1212")
## newey west
newey_west<-function(x){
model<-lm(x~1)
newey_west<-coeftest(model,vcov=NeweyWest(model,verbose=T))
newey_west[c(1,3,4)]
}
## running newey west
cs_nw_full<-do.call(cbind,lapply(tsmom,newey_west))
library(gtools)
p_values<-cs_nw_full[3,]
cs_nw_full[2,]<-paste0(cs_nw_full[2,],stars.pval(p_values))
write.xlsx(cs_nw_full,"list_file.xlsx")
}
Try:
write.xlsx(cs_nw_full, paste0(eval(substitute(list_files)), ".xlsx"))
Edit:
#jeetkamal is absolutely right - you need to use
write.xlsx(cs_nw_full, paste0(deparse(substitute(list_files)), ".xlsx"))
here.
I apologize for the mistake. eval wold only work if list_files was e.g. the name of a file, not a list object.

Key binding by file type

Description
I'm trying to create a key binding that behaves differently based on the file type.
Ideally what id like to do is the following:
If the file type is .md then run the command markdown-preview-plus:toggle
else run the command script:run
I know it's something along the lines of:
file init.coffee :
editor.command('custom:command', e => {
if ( of file type .md) {
markdown-preview-plus:toggle
} else {
script:run
}
})
Then in the keymap.cson i have to add something like:
'atom-text-editor':
'cmd-i': 'custom:command'
But obviously this is pseudocode. I've tried reading the documentation specifically this
but there isn't enough information.
I was able to do this by adding the following to the keymap.cson file:
"atom-text-editor[data-grammar='source gfm']":
'cmd-i': 'markdown-preview-plus:toggle'
"atom-text-editor:not([data-grammar='source gfm'])":
'cmd-i': 'script:run'
For anyone trying to do something similar to this, I used this as reference:
Atom grammer syntax

Concatenate variables in R

I want to create an object in R, which will contain one string, with a few variables (in my case it is file path). When I try to use paste to concatenate the file paths I can see only one last variable instead of all variables in one string. I use next code:
for(i in seq_len(nrow(samples))) {
lib = samples$conditions[i]
txtFile = file.path(lib, "hits.txt")
testfiles = paste(txtFile, sep = ',')
}
print(testfiles)
and get something like
cond/hits.txt,
instead of
cond/hits.txt,cond1/hits.txt,cond2/hits.txt and so on
Thank you very much for help

Resources