1

I'd like to change the values in a data.frame that match a criteria, but I'd like to specify the criteria only applies to certain columns. I'm aware about:

  • Being df = Dataframe: df[df < 0] = 0. This applies to all elements in all the columns, so that, this not resolve my problem
  • Being df = Dataframe and Col: a dataframe's column: df$Col[df$Col < 0] = 0.This applies to all elements in 1 column, so that, this not resolve my problem neither.

Which is the solution in the middle that allows me to filter all elements for those specified columns that I define?

4

2 に答える 2

2

特定の列がインデックスとして利用可能であると仮定します。

cols <- 1:750
df[,cols][df[,cols] < 0] = 0

一部の列を除外する必要がある場合は、負のインデックスを使用します。例:

cols <- c(-3,-4)
于 2013-04-04T08:10:37.110 に答える