I'm interested in Using results from the R console and R code to generate the name of an object into R script.
I created a linear regression with 1 IV, where the DV is mpg
and the 2 IVs of interest are cyl
and disp
. I call the generic model code object lm_DVmpg
. I used the code paste("lm_DVmpg","_IV", as.character(lm_DVmpg$call$formula[[3]]), sep = "")
to generate a unique name for the object containing the lm() in the console. I then pasted the result into the script and used that text as the name of the IV specific regression object.
For example, the name of the object using the IV of cyl
is lm_DVmpg_IVcyl
, and the name of the object disp
is lm_DVmpg_IVdisp
.
Here is some example code from the console:
> ## first lm
> # ---- NOTE: Iv is cyl
> # ---- NOTE: creates lm() object
> lm_DVmpg <- lm(mpg~cyl, data = mtcars)
> # ---- NOTE: creates unique name for lm() object
> paste("lm_DVmpg","_IV", as.character(lm_DVmpg$call$formula[[3]]), sep = "")
[1] "lm_DVmpg_IVcyl"
> # ---- NOTE: moves lm_DVmpg to object unique to IV used
> lm_DVmpg_IVcyl <- lm_DVmpg
Is there any way to use R code in the R script file to change the name of the lm_DVmpg
code in the line lm_DVmpg <- lm(mpg~cyl, data = mtcars)
say to lm_DVmpg_IVcyl
, derived from the paste("lm_DVmpg","_IV", as.character(lm_DVmpg$call$formula[[3]]), sep = "")
command, using R code without having to copy/paste the text lm_DVmpg_IVcyl
from the console to the appropriate R script line?
Alternatively, is there a way to get the text lm_DVmpg_IVcyl
into the line lm_DVmpg_IVcyl <- lm_DVmpg
, derived from the paste("lm_DVmpg","_IV", as.character(lm_DVmpg$call$formula[[3]]), sep = "")
command, using R code without having to copy/paste from the console after running the code paste("lm_DVmpg","_IV", as.character(lm_DVmpg$call$formula[[3]]), sep = "")
?
Any help would be appreciated.
FYI, I use a 2013 Macbook Pro with a 2.4 GHz dual-core intel chip, 8 GB of ram, macOS big sur 11.2.2, RStudio Version 1.4.1106, and the R Base Package 4.04.
Thanks.
The R script I used is below:
# Naming objects from returns on console
# ---- NOTE: will do several linear regressions using the mtcars dataset, with IV and the DV being "mpg"
# ---- NOTE: IVs - cyl, disp
## gives info on dataset
head(mtcars)
str(mtcars)
colnames(mtcars)
## first lm
# ---- NOTE: Iv is cyl
# ---- NOTE: creates lm() object
lm_DVmpg <- lm(mpg~cyl, data = mtcars)
# ---- NOTE: creates unique name for lm() object
paste("lm_DVmpg","_IV", as.character(lm_DVmpg$call$formula[[3]]), sep = "")
# ---- NOTE: moves lm_DVmpg to object unique to IV used
lm_DVmpg_IVcyl <- lm_DVmpg
## second lm
# ---- NOTE: Iv is disp
# ---- NOTE: creates lm() object
lm_DVmpg <- lm(mpg~disp, data = mtcars)
# ---- NOTE: creates unique name for lm() object
paste("lm_DVmpg","_IV", as.character(lm_DVmpg$call$formula[[3]]), sep = "")
# ---- NOTE: moves lm_DVmpg to object unique to IV used
lm_DVmpg_IVdisp <- lm_DVmpg
## displays summaries of regression objects
# ---- NOTE: latest regression object summary
summary(lm_DVmpg)
# ---- NOTE: unique regression objects
lm_DVmpg_IVmpg
lm_DVmpg_IVdisp
# ---- NOTE: lm_DVmpg = lm_DVmpg_IVdisp, since the lm_DVmpg_IVmpg object when IV == disp was the latest lm() to be run
Here are my results from the console when running the R script.
> # Naming objects from returns on console
> # ---- NOTE: will do several linear regressions using the mtcars dataset, with IV and the DV being "mpg"
> # ---- NOTE: IVs - cyl, disp
> # Naming objects from returns on console
> # ---- NOTE: will do several linear regressions using the mtcars dataset, with IV and the DV being "mpg"
> # ---- NOTE: IVs - cyl, disp
>
> ## gives info on dataset
> head(mtcars)
mpg cyl disp hp drat wt qsec vs am gear carb
Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4
Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4
Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1
Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1
Hornet Sportabout 18.7 8 360 175 3.15 3.440 17.02 0 0 3 2
Valiant 18.1 6 225 105 2.76 3.460 20.22 1 0 3 1
> str(mtcars)
'data.frame': 32 obs. of 11 variables:
$ mpg : num 21 21 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 ...
$ cyl : num 6 6 4 6 8 6 8 4 4 6 ...
$ disp: num 160 160 108 258 360 ...
$ hp : num 110 110 93 110 175 105 245 62 95 123 ...
$ drat: num 3.9 3.9 3.85 3.08 3.15 2.76 3.21 3.69 3.92 3.92 ...
$ wt : num 2.62 2.88 2.32 3.21 3.44 ...
$ qsec: num 16.5 17 18.6 19.4 17 ...
$ vs : num 0 0 1 1 0 1 0 1 1 1 ...
$ am : num 1 1 1 0 0 0 0 0 0 0 ...
$ gear: num 4 4 4 3 3 3 3 4 4 4 ...
$ carb: num 4 4 1 1 2 1 4 2 2 4 ...
> colnames(mtcars)
[1] "mpg" "cyl" "disp" "hp" "drat" "wt" "qsec" "vs" "am" "gear" "carb"
>
> ## first lm
> # ---- NOTE: Iv is cyl
> # ---- NOTE: creates lm() object
> lm_DVmpg <- lm(mpg~cyl, data = mtcars)
> # ---- NOTE: creates unique name for lm() object
> paste("lm_DVmpg","_IV", as.character(lm_DVmpg$call$formula[[3]]), sep = "")
[1] "lm_DVmpg_IVcyl"
> # ---- NOTE: moves lm_DVmpg to object unique to IV used
> lm_DVmpg_IVcyl <- lm_DVmpg
>
> ## second lm
> # ---- NOTE: Iv is disp
> # ---- NOTE: creates lm() object
> lm_DVmpg <- lm(mpg~disp, data = mtcars)
> # ---- NOTE: creates unique name for lm() object
> paste("lm_DVmpg","_IV", as.character(lm_DVmpg$call$formula[[3]]), sep = "")
[1] "lm_DVmpg_IVdisp"
> # ---- NOTE: moves lm_DVmpg to object unique to IV used
> lm_DVmpg_IVdisp <- lm_DVmpg
>
> ## displays summaries of regression objects
> # ---- NOTE: latest regression object summary
> summary(lm_DVmpg)
Call:
lm(formula = mpg ~ disp, data = mtcars)
Residuals:
Min 1Q Median 3Q Max
-4.8922 -2.2022 -0.9631 1.6272 7.2305
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 29.599855 1.229720 24.070 < 2e-16 ***
disp -0.041215 0.004712 -8.747 9.38e-10 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Residual standard error: 3.251 on 30 degrees of freedom
Multiple R-squared: 0.7183, Adjusted R-squared: 0.709
F-statistic: 76.51 on 1 and 30 DF, p-value: 9.38e-10
> # ---- NOTE: unique regression objects
> lm_DVmpg_IVmpg
Call:
lm(formula = mpg ~ cyl, data = mtcars)
Coefficients:
(Intercept) cyl
37.885 -2.876
> lm_DVmpg_IVdisp
Call:
lm(formula = mpg ~ disp, data = mtcars)
Coefficients:
(Intercept) disp
29.59985 -0.04122
> # ---- NOTE: lm_DVmpg = lm_DVmpg_IVdisp, since the lm_DVmpg_IVmpg object when IV == disp was the latest lm() to be run
I am not sure why you need this but I think assign
+ get
is what you are looking for.
lm_DVmpg <- lm(mpg~cyl, data = mtcars)
new_name <- paste("lm_DVmpg","_IV", as.character(lm_DVmpg$call$formula[[3]]), sep = "")
assign(new_name, lm(mpg~disp, data = mtcars))
get(new_name)
#Call:
#lm(formula = mpg ~ disp, data = mtcars)
#Coefficients:
#(Intercept) disp
# 29.59985 -0.04122
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.