R -使用with()从dataframe创建新变量

时间:2022-07-13 22:56:41

I learned one should avoid using attach. And to use with() instead. However, sometimes it seems that is not a solution in all cases.

我学过应该避免使用attach。用()代替。然而,有时这似乎并不是所有情况下的解决方案。

I need to create several new variable (matrices), caclulated from one and the same value in a data.frame:

我需要创建几个新的变量(矩阵),从一个和相同的值在一个数据。frame:

df1 <- data.frame(Var1=rnorm(50,5), Var2=rnorm(50,12))

q5 <- quantile(df1$Var1,.05, na.rm=TRUE)# lower quantile
q95 <- quantile(df1$Var1,.95, na.rm=TRUE) # upper quantile
medx <- median(df1$Var1, na.rm=TRUE) # median
x.dens <- density(df1$Var1, na.rm=TRUE) # density

I want to avoid the burdensome/redundant use of "data.frame$"

我想避免使用“data.frame$”这种繁琐/重复的用法

but this does not work:

但这行不通:

with(df1, 
q5 <- quantile(Var1,.05, na.rm=TRUE),# lower quantile
q95 <- quantile(Var1,.95, na.rm=TRUE), # upper quantile
medx <- median(Var1, na.rm=TRUE), # median
x.dens <- density(df1$Var1, na.rm=TRUE)) # density

indeed, it is repeatedly the same operation, with the same settings, but different function.

的确,它重复着相同的操作、相同的设置、不同的功能。

Do you know how to overcome the elaborate use of code

你知道如何克服复杂的代码使用吗

2 个解决方案

#1


0  

If you want the same results as your 'data.frame$' containing code by using with, you should explicitly assign the values to the global environment (Otherwise the values will be assigned in data environment and will not be returned):

如果您希望使用with来获得与包含代码的“data.frame$”相同的结果,您应该显式地将值分配给全局环境(否则值将在数据环境中分配,不会返回):

with(df1,{ 
     q5 <<- quantile(Var1,.05, na.rm=TRUE) # lower quantile
     q95 <<- quantile(Var1,.95, na.rm=TRUE)  # upper quantile
     medx <<- median(Var1, na.rm=TRUE)  # median
     x.dens <<- density(df1$Var1, na.rm=TRUE)}) # density

The <<- function will do it.

<- function会这样做。

#2


2  

use within() instead of with when you create a variable in the data frame environment.

在()中使用,而不是在数据框架环境中创建变量时使用。

res <- within(df1, 
              q5 <- quantile(Var1,.05, na.rm=TRUE),# lower quantile
              q95 <- quantile(Var1,.95, na.rm=TRUE), # upper quantile
              medx <- median(Var1, na.rm=TRUE), # median
              x.dens <- density(df1$Var1, na.rm=TRUE)) # density

head( res)
#      Var1     Var2       q5
# 1 4.943871 12.29145 3.678482
# 2 4.844204 11.55671 3.678482
# 3 3.529248 12.00111 3.678482
# 4 4.521850 12.07434 3.678482
# 5 5.417942 11.41048 3.678482
# 6 6.358680 11.43133 3.678482

#1


0  

If you want the same results as your 'data.frame$' containing code by using with, you should explicitly assign the values to the global environment (Otherwise the values will be assigned in data environment and will not be returned):

如果您希望使用with来获得与包含代码的“data.frame$”相同的结果,您应该显式地将值分配给全局环境(否则值将在数据环境中分配,不会返回):

with(df1,{ 
     q5 <<- quantile(Var1,.05, na.rm=TRUE) # lower quantile
     q95 <<- quantile(Var1,.95, na.rm=TRUE)  # upper quantile
     medx <<- median(Var1, na.rm=TRUE)  # median
     x.dens <<- density(df1$Var1, na.rm=TRUE)}) # density

The <<- function will do it.

<- function会这样做。

#2


2  

use within() instead of with when you create a variable in the data frame environment.

在()中使用,而不是在数据框架环境中创建变量时使用。

res <- within(df1, 
              q5 <- quantile(Var1,.05, na.rm=TRUE),# lower quantile
              q95 <- quantile(Var1,.95, na.rm=TRUE), # upper quantile
              medx <- median(Var1, na.rm=TRUE), # median
              x.dens <- density(df1$Var1, na.rm=TRUE)) # density

head( res)
#      Var1     Var2       q5
# 1 4.943871 12.29145 3.678482
# 2 4.844204 11.55671 3.678482
# 3 3.529248 12.00111 3.678482
# 4 4.521850 12.07434 3.678482
# 5 5.417942 11.41048 3.678482
# 6 6.358680 11.43133 3.678482