R数据处理 – 自定义方法

需求:v<-c(1,2,3,4,5,5,5,5),得到打分为5的数字。

#1. 基本操作法

(v[v==5])

 

#2. 通过function封装后,Filter调用

isGoodNumber<-function(x){
if (x==5) return(TRUE)
else return(FALSE)
}

(Filter(isGoodNumber,v))

 

#3.通过function封装后,mapply调用

(v[mapply(isGoodNumber,v)])

 

#4.  通过function封装后,Vectorize变换function,再调用

(v[Vectorize(isGoodNumber)(v)])

This entry was posted in R语言实践

0 0 votes
Article Rating
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x