Home > database >  How to cutoff values in dataframe?
How to cutoff values in dataframe?

Time:10-06

From the following dataframe:

df1 <- data.frame(id=c(1, 2, 3, 4, 5, 6, 7),
                  revenue=c(34, 1000, 40, 49, 43, 55, 99))
df2 <- data.frame(id=c(1, 2, 3, 4, 5, 6, 7),
                  expenses=c(22, 26, 31, 40, 20, 2000, 22))
df3 <- data.frame(id=c(1, 2, 3, 4, 5, 6, 7),
                  profit=c(12, 10, 14, 12, 9, 15, 16))
df_list <- list(df1, df2, df3)    
test <- Reduce(function(x, y) merge(x, y, all=TRUE), df_list)  
rownames(test) <- test[,1]
test[,1] <- NULL
test

I would like to eliminate extreme values (e.g. 1000 and 2000). I need to cutoff everything that is greater than 100. When I check test<100 I see TRUE and FALSE positions but I would like to replace them with NA or zeroes.

CodePudding user response:

To replace all values in a dataframe (df) which values are higher than 100 with a 0 simply use: df[df > 100] = 0

CodePudding user response:

We can use replace()

replace(test, test>100, NA)

  revenue expenses profit
1      34       22     12
2      NA       26     10
3      40       31     14
4      49       40     12
5      43       20      9
6      55       NA     15
7      99       22     16
  •  Tags:  
  • r
  • Related