Example:
var<-"name"
setwd("/media/data/var")
gives an error 'cause directory "/media/data/var" does not exist, but "/media/data/name".
So, how to declare var as variable within a quoted string?
You have to use paste:
setwd(paste0("/media/data/",var))
Or you can use file.path:
setwd(file.path("/media/data",var))
Related
Let's say we have a url in R like:
url <- 'http://google.com/maps'
And the objective is to change the 'maps' part of it. I'd like to write a function where basically I can just input something (e.g. 'maps', 'images'), etc., and the relevant part of the url will automatically change to reflect what I'm typing in.
Is there a way to do this in R, where part of the url can be changed by typing something into a function?
Thanks!
You have to store the part you type into a variable and paste this to the base URL:
base_url <- "http://google.com/"
your_extension <- "maps"
paste0(base_url, your_extension)
[1] "http://google.com/maps"
If you have to start with a fixed URL, use sub to replace the last part:
sub("\\w+$", 'foo', url)
# "http://google.com/foo"
You can use dirname to remove the last part of the URL and paste it with additional custom string.
change_url_part <- function(base_url, string) {
paste(dirname(base_url), string, sep = '/')
}
change_url_part('http://google.com/maps', 'images')
#[1] "http://google.com/images"
A variable is defined somewhere
$somevar: 'red';
Then I want to access this var but I only have the varname given as a string.
$varname: 'somevar';
What I want is something like:
$varvalue: getValueOfVar($varname);
you can do like this
$somevar: red;
and then
$varname: $somevar;
here is working example
I would like to train a model and give it a name. I would like to use this name as character as well to create a text file with model summary. So I created a function as below
C50Training<-function(ModeName,DF_Trai,Form,
Str_PathSum){
library(C50);
ModeName<-C5.0(formula=Form,data=DF_Trai);
capture.output(summary(ModeName),file=paste(Str_PathSum,"/Summ",ModeName,".txt",sep=""));
}
In the funtion I want to use ModeName as characters. I tried to run it but it does not work. ModelName is a list in this case. How can I use ModelName as character?
To change a variable name to string, you can use deparse and substitute, as follows:
deparse(substitute(ModeName))
It return "ModeName" that can be part of your file path.
I tried this. It works.
ModeName=c(1,2,3)
f<-function(ModeName){
print(paste("/Summ",deparse(substitute(ModeName)),".txt",sep=""))
}
f(ModeName)
and this works too:
ModeName=c(1,2,3)
f<-function(list){
print(paste("/Summ",deparse(substitute(list)),".txt",sep=""))
}
f(ModeName)
I try to group by my customize movieLense dataset
groupBy<- data.table(unifiedTbl)
x<- groupBy[,list(rating=sum(rating)
,Unknown=sum(unknown)
,Action=sum(Action)
,Adventure = sum(Adventure)
,Animation = sum(Animation)
,"Children's" = sum(Children's)
),by=list(user_id,age,occupation)]
but because of Children's I received some error which related to specified character
If I remove below part of my code every things is OK
,"Children's" = sum(Children's)
Now my question is how can I address to this column with full name?
how can I fix my codes?
You can use backticks with names that aren't valid syntax:
`Children's` = sum(`Children's`)
And of course, I'd recommend creating valid names instead:
setnames(groupBy, make.names(names(groupBy)))
in R,the pdf function can save graph in c:/test:
pdf("c:/test")
I want to make a variable substitue pdf ,how can i make it run ?
str<-"pdf"
str("c:/test")
get() does this:
get(str)("c:/test")
s = "pdf" ; do.call(s, list("c:/test"))
or in two steps,
cl <- call(s, "c:/test")
eval(cl)
You can extract the function specified by the name in str with match.fun:
match.fun(str)("c:/test")
By the way: It is not a good idea to name an object str since this is the name of a basic function in R.