Creating a flagging variable by SD

Hi! I'm trying to create a variable that will let me know if an individual row has an SD below a certain cutoff. The SD cutoff is very high here because most of my data is good. Right now the issue is that I'm getting a column for SD with a very long name, and all my NFC.flag and flag.count variables are showing as 1 even though they shouldn't be based on the data.

Here is my data set:

DF_NFC <- data.frame(
NFC1 = c(4, 5, 2, 4, 2, 1),
NFC2 = c(5, 5, 3, 4, 4, 1),
NFC3 = c(4, 4, 2, 3, 4, 2),
NFC4 = c(5, 4, 3, 1, 4, 1),
NFC5 = c(4, 5, 4, 3, 4, 3),
NFC6 = c(4, 4, 4, 4, 4, 2),
NFC7 = c(5, 4, 2, 3, 2, 3),
NFC8 = c(2, 4, 4, 1, 3, 1),
NFC9 = c(5, 4, 4, 2, 4, 1),
NFC10 = c(5, 5, 2, 5, 4, 1),
NFC11 = c(4, 5, 1, 2, 4, 1),
NFC12 = c(4, 4, 4, 2, 5, 2),
NFC13 = c(5, 4, 3, 5, 4, 3),
NFC14 = c(4, 5, 4, 4, 4, 3),
NFC15 = c(5, 4, 4, 3, 4, 1),
NFC16 = c(4, 3, 2, 2, 3, 1),
NFC17 = c(5, 4, 4, 4, 5, 3),
NFC18 = c(4, 5, 3, 5, 3, 2)
)

Here is the current code:

t <- 1 #sd threshold

NFCflag <- DF_NFC %>%
rowwise() %>%
mutate((NFC.sd = sd(c_across(NFC1:NFC18))),
NFC.flag = case_when(abs(NFC.sd) >= t ~ 0,
abs(NFC.sd) < t ~ 1),
flag.count = sum(c(NFC.flag)))

The long name is due to an extra set of parentheses in the first line of mutate
Also, IMO, the case_when is unneeded, I would just make a logical comparison.
That NFC.flag is all 1 is because there are no matches for a column named NFC.sd, due to the wrong name.

My approach would look like this

DF_NFC %>%
     rowwise() %>%
     mutate(NFC.sd = sd(c_across(NFC1:NFC18)),
            NFC.flag = NFC.sd > t)  # wrap in as.integer if you prefer 1 / 0

Awesome! Thank you. This works well for me :grinning: