R_Language 板


LINE

※ 引述《cog5566 (刃之56)》之銘言: : ※ 引述《Chris7462 (~烤焦麵包~)》之銘言: : : subsetBy(formula, subset, data = parent.frame(), select, drop=FALSE, : : join=TRUE, ... ) : : 第二個參數是 logical 不是 expression,可以看他的說明 : : subset logical expression indicating elements or rows to keep: missing values : : taken as false. : : 用他的 example 看一下就知道了 : : sbstBy> data(dietox) : : sbstBy> subsetBy(~Evit, Weight < mean(Weight), data=dietox) : : data(dietox) : : dietox$Weight < mean(dietox$Weight) : 我想不是 logical 是 logical expression : 你看例子的第二個參數是 Weight < mean(Weight) : 並不是 dietox$Weight < mean(dietox$Weight) 你只看到了第二個參數,沒看到後面的 data=dietox subsetBy 括號裡面整個的意思是用 dietox 這組資料的 Weight 也就是 dietox$Weight 可以把資料 attach 上去就知道了。 > data(dietox) > attach(dietox) > typeof(Weight < mean(Weight)) [1] "logical" > typeof(dietox$Weight < mean(dietox$Weight)) [1] "logical" > Weight == dietox$Weight 兩個是一樣的 : 如果你用 : typeof(Weight < mean(Weight)) : 和 : typeof(dietox$Weight < mean(dietox$Weight)) : 會得到完全不一樣的結果 因為你開 R 的時候有讀到舊的資料或是 R 的暫存檔,沒有 attach dietox 這個 data 的話應該不會 Weight 這個 variable 請先把舊的暫存檔或舊資料清掉,不然這樣討論下去你只會一直得到很奇怪的結果 : Weight < mean(Weight) 會變成 logical class 會是在 subsetBy 自己的 scope 之內 : 的事,但是在 subsetBy 之外的時候狀況很奇怪。 : 這也是一個我很不習慣 R 的地方之一。有一些 function 會展現出奇怪的特性, : 就是你不知道到底 function 是抓引數的 value 還是 name or expression : 譬如說好了 : x <- 10 : rm('x') 和 rm(x) : 有同樣的效果 請看 ?rm rm (..., list = character(), pos = -1, envir = as.environment(pos), inherits = FALSE) ... the objects to be removed, as names (unquoted) or character strings (quoted). 裡面放的可以是變數名稱或是字串,所以 rm('x') 跟 rm(x) 一樣 : 看起來好像沒有很奇怪,但是爲什麽 rm(x) 不會變成 rm(10)呢? : 下面這個例子就會更奇怪 : x <- 10 : y <- 'x' : rm(y) : 那到底這個時候是什麽情況,到底這時候會抓 rm 的參數 name y 還是 value 'x'? 請把值呼叫出來 > x # x 是變數,數值為 10 [1] 10 > y # y 是變數,數值是字元 "x" [1] "x" : 回到我的問題的例子 : subsetBy(~Evit, Weight < mean(Weight), data=dietox) : 如果在一般的 function下,而且 Weight 有給予值時, : Weight < mean(Weight) 應該會去抓 Weight 的值來得出 logical class : 但是這邊等神奇之處就是,Weight 是 dietox 的一個 column, : 或是複雜一點的說,是 dietox 這個 data frame 的 environment 裏面的變數 : 他們是在 subsetBy 之內才被組合的。 : 這樣一來,subsetBy 必定會知道 subsetBy 外面被 call 時引數的 expression : 而不是值。現在知道這可以用 substitute 來實現,但是我的問題是如果有下一層, : 如何將這個 expression 帶到下一層? : 我還是會不行用耶,貼上我的 code : ###################################################################### : library('doBy') : sdSubsetFun = function(group, dVar, data) { : # 2SD threshold : subsetThreshold = (dVar > (mean(dVar) - 2*sd(dVar))) & : (dVar < (mean(dVar) + 2*sd(dVar))) : dataOut = subsetBy(group, : subset = subsetThreshold, : data = data) 問題是出在這邊,因為 data = data 用的是 data 這個資料的 data$subsetThreshold 但是 data 下並沒有 data$subsetThreshold 這個變數,所以就有 error : } : x = rep(c('a','b','c'), 50) : y = 1:150 : tData = data.frame(aa = x, bb = y) 另外這邊也錯,兩個改法。一個是 > tData = data.frame(aa <- x, bb <- y) 或是 > tData = data.frame(aa = x, bb = y) > aa <- x > bb <- y : subData = sdSubsetFun(~aa, bb, tData) : ###################################################################### : Error in sdSubsetFun(~aa, bb, tData) : object 'bb' not found : ###################################################################### : 其實很顯而易見,在 evaluate dVar 的時候會在 local environment 找不到值, : 因為 dynamic scope 的關係,它會去 global environment 找,但也會找不到。 : 所以他會再找 bb 的value,但是根本就沒有 bb 這個變數,bb是tData的 column name : 所以最後他會說 bb 找不到。 : 還是說我什麽地方弄錯了?可能有小細節沒有注意到。 看不懂上面的幾個錯誤跟 dynamic scope 有什麼關係? 整的問題只是呼叫 function 的時候沒有把該放的參數放對而已。 我把整個改過的程式貼上來如下,如果再不行我也不知道怎麼幫你了.... library('doBy') sdSubsetFun = function(group, dVar, data) { # 2SD threshold data$subsetThreshold = (dVar > (mean(dVar) - 2*sd(dVar))) & (dVar < (mean(dVar) + 2*sd(dVar))) dataOut = subsetBy(group, subset = subsetThreshold, data = data) } x = rep(c('a','b','c'), 50) y = 1:150 tData = data.frame(aa = x, bb = y) aa <- x bb <- y subData = sdSubsetFun(~aa, bb, tData) --



※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 36.229.4.177
1F:→ Chris7462:另外建議 x = rep(c('a','b','c'), 50) 改成 factor 06/02 13:22
2F:→ Chris7462:x <- factor(rep(c('a','b','c'), 50)) 會比較好 06/02 13:22
※ 編輯: Chris7462 來自: 118.165.62.51 (06/03 01:54)







like.gif 您可能會有興趣的文章
icon.png[問題/行為] 貓晚上進房間會不會有憋尿問題
icon.pngRe: [閒聊] 選了錯誤的女孩成為魔法少女 XDDDDDDDDDD
icon.png[正妹] 瑞典 一張
icon.png[心得] EMS高領長版毛衣.墨小樓MC1002
icon.png[分享] 丹龍隔熱紙GE55+33+22
icon.png[問題] 清洗洗衣機
icon.png[尋物] 窗台下的空間
icon.png[閒聊] 双極の女神1 木魔爵
icon.png[售車] 新竹 1997 march 1297cc 白色 四門
icon.png[討論] 能從照片感受到攝影者心情嗎
icon.png[狂賀] 賀賀賀賀 賀!島村卯月!總選舉NO.1
icon.png[難過] 羨慕白皮膚的女生
icon.png閱讀文章
icon.png[黑特]
icon.png[問題] SBK S1安裝於安全帽位置
icon.png[分享] 舊woo100絕版開箱!!
icon.pngRe: [無言] 關於小包衛生紙
icon.png[開箱] E5-2683V3 RX480Strix 快睿C1 簡單測試
icon.png[心得] 蒼の海賊龍 地獄 執行者16PT
icon.png[售車] 1999年Virage iO 1.8EXi
icon.png[心得] 挑戰33 LV10 獅子座pt solo
icon.png[閒聊] 手把手教你不被桶之新手主購教學
icon.png[分享] Civic Type R 量產版官方照無預警流出
icon.png[售車] Golf 4 2.0 銀色 自排
icon.png[出售] Graco提籃汽座(有底座)2000元誠可議
icon.png[問題] 請問補牙材質掉了還能再補嗎?(台中半年內
icon.png[問題] 44th 單曲 生寫竟然都給重複的啊啊!
icon.png[心得] 華南紅卡/icash 核卡
icon.png[問題] 拔牙矯正這樣正常嗎
icon.png[贈送] 老莫高業 初業 102年版
icon.png[情報] 三大行動支付 本季掀戰火
icon.png[寶寶] 博客來Amos水蠟筆5/1特價五折
icon.pngRe: [心得] 新鮮人一些面試分享
icon.png[心得] 蒼の海賊龍 地獄 麒麟25PT
icon.pngRe: [閒聊] (君の名は。雷慎入) 君名二創漫畫翻譯
icon.pngRe: [閒聊] OGN中場影片:失蹤人口局 (英文字幕)
icon.png[問題] 台灣大哥大4G訊號差
icon.png[出售] [全國]全新千尋侘草LED燈, 水草

請輸入看板名稱,例如:BabyMother站內搜尋

TOP