R内のテーブルの各列の要素をカウントする方法次のようになり、私は、データセットを持って
data <- read.csv("sample.csv")
subject gender age type satisfation agree
1 f 22 a yes yes
2 f 23 b no yes
3 f 21 b no
4 m 24 c yes yes
5 f 22 b no yes
6 m a yes yes
7 25 c yes no
8 m 21 b no yes
9 f 23 c yes yes
私はカウントしたいと思います(実際には> 50個の列があります)各列の要素(NAを数えない)とカウントするように
subject gender age type satisfation agree
9 8 8 9 8 9
私は、スクリプトを書いた
以下のレイアウトとして結果をエクスポート
それはすべて0dput(head(data, 9))
structure(list(subject = 1:9, gender = structure(c(2L, 2L, 2L,
3L, 2L, 3L, 1L, 3L, 2L), .Label = c("", "f", "m"), class = "factor"),
age = c(22L, 23L, 21L, 24L, 22L, NA, 25L, 21L, 23L), type = structure(c(1L,
2L, 2L, 3L, 2L, 1L, 3L, 2L, 3L), .Label = c("a", "b", "c"
), class = "factor"), satisfation = structure(c(3L, 2L, 1L,
3L, 2L, 3L, 3L, 2L, 3L), .Label = c("", "no", "yes"), class = "factor"),
agree = structure(c(2L, 3L, 1L, 3L, 2L, 3L, 1L, 3L, 2L), .Label = c("no",
"yes", "yes "), class = "factor"), time = c(23L, 54L, 67L,
324L, 87L, 12L, 756L, 34L, 98L), day = c(1L, 3L, 2L, 5L,
7L, 4L, 3L, 1L, 4L)), .Names = c("subject", "gender", "age",
"type", "satisfation", "agree", "time", "day"), row.names = c(NA,
9L), class = "data.frame")
を出したので、私はうまくいかなかった
counting <- function(x) {
for(i in 1:length(data)) {
data <- length(which(!is.na(x$i)))
print(data)
}
return(data)
}
counting(data)
は、スクリプトの任意の勧告はplzは、ありますか?
ありがとうございました!
'x $ i'と入力して結果を調べる...' sapply'や何かをここで使うか、単に 'summary'(NAカウントを報告する)を使うといいでしょう。 – Frank
@Frankあなたが意味するものを得る。要約はカウントしたくないNAを数える:( – PNY
私はあなたが正しい構文を使用していないことを確認するために 'x $ i'と打ち込んだのですか?そして第二に、' summary'はあなたが望む情報を表示します(あなたが 'sapply'から得ることができる)あなたの好みのフォーマットではありません。まだCSVからデータを読み込んでいない場合は、この問題を試みる準備ができていないかもしれませんが、 Dput(DF) 'を呼び出して、ここに結果をコピーしてください。 – Frank