[問題] 在變數中找出factor

作者: AmigoSafin   2018-11-30 10:42:38
[問題類型]:
程式諮詢(我想用R 做某件事情,但是我不知道要怎麼用R 寫出來)
[軟體熟悉度]:
入門(寫過其他程式,只是對語法不熟悉)
[問題敘述]:
請簡略描述你所要做的事情,或是這個程式的目的
想請教大家
已知一個data frame內有1000個變數
寫code找出其中為factor的變數
( You may choose to create a data frame but it is not part of the
requirement)
得到的解答是這樣:
sum(sapply(x,is.factor))
getNFactors<-function(x) sum(sapply(x,is.factor))
y<-data.frame(a=letters[1:2],b=1:2,d=c("A","Factor?"),e=2:3)
getNFactors(y) # 2
y$d<-as.character(y$d)
getNFactors(y) # 1
實話說實在看不太懂最後這一部分
請問是if getNFactors(y)=2則其中沒有Factors
if getNFactors(y)=1則為有 Factors
因為原程式碼用d=c("A", "Factors?")來測試
但b=1:2, e=2:3
是什麼意思呢?
謝謝大家~~
作者: obarisk (OSWALT)   2018-11-30 15:49:00
1:2 == c(1, 2)

Links booklink

Contact Us: admin [ a t ] ucptt.com