0

いくつかの負の値を持つ store という数値列を持つデータ フレームがあります。マイナスに1440を足したいのですが、困っています。私のデータは次のようになります。

   score
  1  816
  2 -200
  3  976
  4 -376
  5    1
  6  121
  7 -331

を使用して値を置き換えることができますtemp[temp$score< 0] <-8888

しかし、: を使用して変数に値を追加しようとするとtemp[temp$score < 0] <- temp$score + 1440、次のような警告が表示されます。

Warning message: In temp$score[temp$score < 0] <- temp$score + 1440 
:number of items to replace is not a multiple of replacement length

そして、いくつかの奇妙な値が返されます。

  score
1   816
2  2256
3   976
4  1240
5     1
6   121
7  2416

関数を間違って呼び出していますか、それともケースの選択が間違っていますか?

4

2 に答える 2

2

コメントで述べたように、NA データがある場合、添字付けは失敗します。

> temp
   score z
1    123 1
2     NA 2
3    345 3
4 -10783 4
5   1095 5
6    873 6
> temp$score[temp$score < 0] <- temp$score[temp$score < 0] + 1440
Error in temp$score[temp$score < 0] <- temp$score[temp$score < 0] + 1440 :  
  NAs are not allowed in subscripted assignments

だから使用which

> temp$score[which(temp$score < 0)] <- temp$score[which(temp$score < 0)] + 1440
> temp
  score z
1   123 1
2    NA 2
3   345 3
4 -9343 4
5  1095 5
6   873 6
于 2012-08-08T07:27:30.943 に答える