R self reference

后端 未结 4 1973
终归单人心
终归单人心 2020-11-28 06:25

In R I find myself doing something like this a lot:

adataframe[adataframe$col==something]<-adataframe[adataframe$col==something)]+1

4条回答
  •  囚心锁ツ
    2020-11-28 06:49

    I'd be partial to (presumably the subset is on rows)

    ridx <- adataframe$col==something
    adataframe[ridx,] <- adataframe[ridx,] + 1
    

    which doesn't rely on any fancy / fragile parsing, is reasonably expressive about the operation being performed, and is not too verbose. Also tends to break lines into nicely human-parse-able units, and there is something appealing about using standard idioms -- R's vocabulary and idiosyncrasies are already large enough for my taste.

提交回复
热议问题