iT邦幫忙

0

[R]請教如何在行中分別比較並計算

r

想請教大大們 小弟剛入學R語言
這邊是小弟現在的語法
想請教如何在行中進行比較
若Di_mv_m中每行的值>=N1則使用公式1;<=N2則使用;介於之間則
維持原本的值
該用甚麼語法會比較好qq
真的非常不好意思小弟會持續精進
https://ithelp.ithome.com.tw/upload/images/20200221/20124354OEAiGM2IjW.jpg
https://ithelp.ithome.com.tw/upload/images/20200221/201243549C4fisXslS.jpg
https://ithelp.ithome.com.tw/upload/images/20200221/20124354BKf8CIWdhi.jpg

圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 個回答

2
sam989798
iT邦新手 5 級 ‧ 2020-02-21 10:02:18
最佳解答

ifelse

x = data.frame(x = c(60,10,30),
               n1 = c(50,50,50),
               n2 = c(20,20,20))

x$d = ifelse(x$x > x$n1 , x$x*5,
             ifelse(x$x < x$n2 , x$x/2 , x$x))

是這個意思嗎? 參考看看

看更多先前的回應...收起先前的回應...
lonionl iT邦新手 5 級 ‧ 2020-02-21 17:58:49 檢舉

真的非常感謝您的解答!!
但有些問題想請教您,在 x$x*5
這個部分小弟想用的是額外的值而不是直接用x去做計算
請問該怎麼寫會比較好呢qq

lonionl iT邦新手 5 級 ‧ 2020-02-21 18:02:22 檢舉

小弟改寫成這樣但找不太到問題..
https://ithelp.ithome.com.tw/upload/images/20200221/20124354AH4Coy2qeg.jpg

lonionl iT邦新手 5 級 ‧ 2020-02-21 18:18:12 檢舉

報告學長小弟發現要改成data.frame的形式就可以用$的語法了!
真的非常感謝您qq
https://ithelp.ithome.com.tw/upload/images/20200221/20124354R4uNVK3wiB.jpg

sam989798 iT邦新手 5 級 ‧ 2020-02-24 08:55:27 檢舉

你可以的XD 加油!

我要發表回答

立即登入回答