作者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