1

私はこのようなものを持っています:

Date        Point1 Point2 Point3
01-03-2000     23     57    98
02-03-2000     67     36    77 
03-03-2000     67     25    47 
... 

のような関数で各列の合計を取得したいのですcolSumsが、最初の列は数値ではなく、文字型です。金額はどうすればいいの?

4

3 に答える 3

5

datあなたのデータフレームである場合は、試すことができます

colSums(dat[ , -1])

これにより、最初の列 (日付のある列) を除く各列の合計が返されます。

于 2012-11-05T07:07:31.327 に答える
4

より一般化可能なソリューション:

# set up some fake data with text and numeric columns 
numchr <- list(1:3,letters[1:3])
test <- data.frame(numchr[sample(1:2,10,replace=TRUE)])
names(test) <- letters[1:10]

# which looks like...
> test
  a b c d e f g h i j
1 1 a a a a 1 a 1 1 a
2 2 b b b b 2 b 2 2 b
3 3 c c c c 3 c 3 3 c

# get the sums only for the numeric columns
colSums(test[sapply(test,is.numeric)])

a f h i 
6 6 6 6
于 2012-11-05T12:20:01.553 に答える
1

別の可能性 (@thelatemail の例を使用):

library(plyr)
numcolwise(sum)(test)
于 2012-11-05T12:36:25.023 に答える