This question already has answers here:
Calculate the mean of one column from several CSV files
(2 answers)
Closed 4 years ago.
I have CSV files named "001", "002",..."100" stored in the working directory. I need to write a function to read any of these files. I tried the function below, but it doesn't work.
func = function(ID)
{
inp = read.csv("ID.csv")
}
I think this is because "ID.csv" is a character whereas ID is a numeric variable, but I am not sure. Can someone please explain the reason and suggest the right code?
Sounds like you sort of understand the problem. "ID.csv" is a string literal and it is literally looking for a file named ID.csv. If I were you, I would input ID as a string like you have it (i.e. "001" instead of 1). Then try this:
func = function(ID)
{
inp = read.csv(paste(ID,".csv",sep=""))
}
Related
This question already has answers here:
Get filename without extension in R
(9 answers)
Find file name from full file path
(4 answers)
Closed 3 years ago.
I have several download links (i.e., strings), and each string has different length.
For example let's say these fake links are my strings:
My_Link1 <- "http://esgf-data2.diasjp.net/pr/gn/v20190711/pr_day_MRI-AGCM3-2-H_highresSST_gn_20100101-20141231.nc"
My_Link2 <- "http://esgf-data2.diasjp.net/gn/v20190711/pr_-present_r1i1p1f1_gn_19500101-19591231.nc"
My goals:
A) I want to have only the last part of each string ended by .nc , and get these results:
pr_day_MRI-AGCM3-2-H_highresSST_gn_20100101-20141231.nc
pr_-present_r1i1p1f1_gn_19500101-19591231.nc
B) I want to have only the last part of each string before .nc , and get these results:
pr_day_MRI-AGCM3-2-H_highresSST_gn_20100101-20141231
pr_-present_r1i1p1f1_gn_19500101-19591231
I tried to find a way on the net, but I failed. It seems this can be done in Python as documented here:
How to get everything after last slash in a URL?
Does anyone know the same method in R?
Thanks so much for your time.
A shortcut to get last part of the string would be to use basename
basename(My_Link1)
#[1] "pr_day_MRI-AGCM3-2-H_highresSST_gn_20100101-20141231.nc"
and for the second question if you want to remove the last ".nc" we could use sub like
sub("\\.nc", "", basename(My_Link1))
#[1] "pr_day_MRI-AGCM3-2-H_highresSST_gn_20100101-20141231"
With some regex here is another way to get first part :
sub(".*/", "", My_Link1)
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'))
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"))
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))) )
}
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")