2017-09-25 126 views
0

与此相关的问题里面: Adding prefixes to some variables without touching others?分配给临时变量的`maggritr`命令链

我尝试这个解决办法,但因为我不知道发生了什么事没有分享:

数据:

df1 <- data.frame("hea_income"=c(45000,23465,89522),"gea_property"=c(1,1,2) ,"win_state"=c("AB","CA","GA"), "education"=c(1,2,3), "commute"=c(13,32,1)) 

溶液:

names(df1) %<>% inset(tmp<-!grepl("_",.),paste0("important_",.)[tmp]) 
# hea_income gea_property win_state important_education important_commute 
# 1  45000   1  AB     1    13 
# 2  23465   1  CA     2    32 
# 3  89522   2  GA     3     1 

我的问题:

tmp 
# Error: object 'tmp' not found 
names(df1) %<>% inset(tmp<-!grepl("_",.),paste0("important_",.)[tmp]) %>% {tmp} 
# Error in function_list[[k]](value) : object 'tmp' not found 

我不完全理解环境与magrittr的工作方式,我很惊喜地看到,tmp不污染我的工作区,但它是一个安全的实践 ?我实际存储tmp某处它浪费内存的地方吗?

如果不是,可能是管用的夜晚招!

回答

0

我不得不回答这个问题:magittr pipe does not correctly pass a connection ...

......要知道,我可以解决这一个矿山自己

我会从指定的最后一个命令当地环境,并将其分配给全球环境,那么我将探讨它:

library(magrittr) 
df1 <- data.frame("hea_income"=c(45000,23465,89522),"gea_property"=c(1,1,2) ,"win_state"=c("AB","CA","GA"), "education"=c(1,2,3), "commute"=c(13,32,1)) 
names(df1) %<>% {assign("scary_place",environment(),envir=globalenv());inset(.,tmp<-!grepl("_",.),paste0("important_",.)[tmp])} 

现在让我们来看看:

ls(scary_place) 
#[1] "tmp" 

所以是的,这很混乱,不要这样做! (太糟糕了......)