1

In R I have the following dataset and I would want remove rows if Per<98, unless no rows with the same value in A meet this condition (then add 0):

df <- cbind(c("D1", "D1", "D1", "D1", "D2", "D2", "D2", "D2", "D3", "D3", "D3", "D3"), c(99.8, 99.5, 98.7, 98, 97.8, 97.3, 96, 95.9, 95, 94.9, 94.5, 94), c("sp", "sp", "sp", "sp", "sp", "sp", "sp", "sp", "sp", "sp", "sp", "sp")) colnames(df) <- cbind("A", "Per", "B")

The expected result would be

df <- cbind(c("D1", "D1", "D1", "D1", "D2", "D3"), c(99.8, 99.5, 98.7, 98, 0, 0), c("sp", "sp", "sp", "sp", "sp", "sp")) colnames(df) <- cbind("A", "Per", "B")

RIA
  • 11
  • 1

1 Answers1

0

This should do the trick.

library(dplyr)
df2=as.data.frame(df, stringsAsFactors=FALSE) %>% mutate(Per=as.numeric(Per))
j=1
for (i in 1:nrow(df2)) {
  print(i)
  if ((sum(df2$A==df2[j,"A"])==1) & (df2[j,"Per"]<98)) {
    df2[j, "Per"]=0
  } else if (df2[j,"Per"] < 98) {
    df2=df2[-j,]
    next
  }
  j=j+1
}
df2

    A  Per  B
1  D1 99.8 sp
2  D1 99.5 sp
3  D1 98.7 sp
4  D1 98.0 sp
8  D2  0.0 sp
12 D3  0.0 sp

If it needs to be a matrix,

df2=as.matrix(df2)
Vons
  • 3,277
  • 2
  • 16
  • 19
  • Many thanks. This is working. With my real database I needed to use mutate(Per=as.numeric(as.character(Per)). Otherwise the numbers were not properly recognized. – RIA Mar 02 '21 at 08:25