Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
423 views
in Technique[技术] by (71.8m points)

dplyr - Fill column with NA if all values are repetead in R

I have a dataframe with significant letters from an ANOVA test. I want to fill the letter column with NA only if all letters are equal (i.e., no significant differences)

I have tried:

library(dplyr)    
df<- data.frame(A=rnorm(5),B=rep("a",5))
df %>%  mutate(B = ifelse(length(unique(B)) == 1,NA, B))

this works for filling with NA but not for re-filling the column with old values:

df2<- data.frame(A=rnorm(5),B=c("b","a","ab","a","b"))
df2 %>%  mutate(B = ifelse(length(unique(B)) == 1,NA, B))
            A B
1  1.67671299 b
2  0.21659428 b
3 -1.50746338 b
4  0.82024729 b
5 -0.01105568 b 

Indeed, it does not fill it again with the original values. I also tried this but it does not work

df2 %>%   mutate_if((length(unique(.)) == 1),list(rep(NA,nrow(df))))
Error: `.p` is invalid.
x `.p` should have the same size as the number of variables in the tibble.
i `.p` is size 1.
i The tibble has 2 columns, non including the grouping variables.

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Since you are doing a scalar comparison here use if/else instead of vectorized ifelse :

library(dplyr)
df2 %>%  mutate(C = if(n_distinct(B) == 1) NA else B)

#           A  B  C
#1  1.6989886  b  b
#2 -0.6174008  a  a
#3  1.3540749 ab ab
#4  1.1774202  a  a
#5  0.8964335  b  b

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...