The specific error you are getting is because your first column is not numeric, yet you are trying to diff
across the whole row. R does not know how to calculate the different between "a" and 1, for example. We need to pass x[-1]
instead of x
to apply
in this case, since we don't want to include the id
column in our diff
calculations.
From your description of the desired output, your definition of "downward trend" is a little odd. If you did a linear regression across each row, then you would have a downward trend in rows with the ids b, c, f, h, l and m:
x$id[apply(x[-1], 1, \(x) lm(x ~ seq_along(x))$coef[2] < 0)]
#> [1] "b" "c" "f" "h" "l" "m"
This, to my mind, is the best definition of "downward trend", but it doesn't quite match what you seem to desire. Instead, your desired output can be obtained by finding the rows where there are no increases across the row and at least one decrease:
x$id[apply(x[-1], 1, \(x) !any(diff(x) > 0) & any(diff(x) < 0))]
#> [1] "c" "f" "h" "l" "m"
Data used
By using an image of your data instead of just including your data as text, means that anyone who wants to help you would have to transcribe your image by hand.
x <- data.frame(id = letters[1:13],
`2` = c(1,1,3,1,1,2,3,9,1,1,1,2,2),
`3` = c(3,2,2,1,1,1,3,4,2,1,1,1,2),
`4` = c(13,0,2,1,1,0,4,0,2,2,1,0,1), check.names = FALSE)