Filter List based on Criteria

Hi, I'm trying to return the names of variables in a dataframe which meet a certain criteria. I get a resulting list but could use help getting the names which meet length = 1 (I'm looking for variables which have unique values of just 1 or length of 1, that is, zero variance)

x1 <- c("T", "T", "T", "T", "T", "T", "F", "F", "F", "N")
x2 <- c("A", "A", "A", "A", "A", "A", "A", "A", "A", "A")
x3 <- c(1,2,3,4,5,6,7,8,9,10)
x4 <- c(1,2,3,4,5,6,7,8,9,10)
d <- data.frame(x1,x2,x3,x4)

v <- d %>% colnames
d1 <- lapply(d[v], unique)
d2 <- lapply(d1[v], length)
d3 <- names(d2)[lapply(d2[v] == 1)] ??

The output should be x2

Thanks in advance for help from smart people


[1] "x2"
1 Like

simple, perfect!


1 Like

If you are happy then "like" the answer and indicate that the issue is closed

1 Like

I see that you have closed the issue and that is nice.
However you indicate that I provided the solution while it was @FJCC who did that.
So next time be precise in choosing the correct answer to close the issue.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.