作者seablack (Water)
看板NCTU-STAT95G
標題Re: [討論] 程式碼
時間Sat Jan 20 12:12:14 2007
How-to use "which" command and avoid using "for-loop" in R.
EX. X = 1 2 3 4 5 6 7 8
If we only want those X < 5 and save them as Y.
One possible solution as below.
> X=c(1,2,3,4,5,6,7,8)
> I=which(X>=5)
> I
[1] 5 6 7 8
# Those I records all "index" of X>=5.
# Next, you can use [-I] to skip thost index which X>=5
# Then, you get a new array Y which are those X<5. ^_^
> Y=X[-I]
> Y
[1] 1 2 3 4
For matrix object, this command also could be applied to.
Other possible commands also could be used to avoid "for-loop in R.
union(x, y)
intersect(x, y)
setdiff(x, y)
setequal(x, y)
Please try to use them. ^___^
※ 引述《ilanglenn (Glenn)》之銘言:
: 我有抄捏
: library 裡 ks
: 不過我不知道怎麼用哈哈
: 還有which
: ex
: I=which(x>0)
: y=x[-I]
: ※ 引述《mangogogo ()》之銘言:
: : 學長我想問一下
: : 就是之前學長在介紹盧sir的最後
: : 有告訴我們可以讓R跑很快的指令
: : 哈哈 我突然忘記了 不過我想把他學起來
: : 學長在教導一下^^ 哈哈
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 61.230.203.108
1F:推 josephw:喔喔喔 謝謝學長喔^^酷耶 01/20 12:12
2F:推 Y0SHIKI:學長好帥:P 01/20 14:55
3F:推 mangogogo:YES~~cool~ 01/20 19:45