-1

私はRで単純な定数をテーブルの列に追加しようとしています.

dim(exampletable)
[1] 3900    2

2 番目の列に値を追加するには、次のようにします。

newtable <- exampletable
for (i in 1:nrow(newtable)){newtable[i,2] <- exampletable[i,2] + constant}

しかし、これは少しやり過ぎのようです。sapply など、よりエレガントな方法はありますか?

ありがとう、ヨハネス

4

2 に答える 2

0

これを試して:

#Dummy data
exampletable <- data.frame(x=runif(3900), y=runif(3900))
#Define new constant
MyConstant <- 10
#Make newtable with MyConstant update
newtable <- exampletable
newtable$y <- newtable$y + MyConstant

これはR言語の基本です。いくつかのマニュアルを読んでください。

于 2013-06-25T10:00:08.537 に答える