I would like to remove the column in which there is an "o"
Var1 Var2 Var3
1 o o o
2 Var1 o o
3 o Var2 o
4 Var1 Var2 o
5 o o o
6 Var1 o o
7 o Var2 o
8 Var1 Var2 o
The problem is that I have several tables and the position of the column that contains the "o" is different in each data frame. I would like to know a way to conditionally remove the columns with "o", since in the different tables the column is in a different position. Could be by evaluating the first row since the whole column is full of "o".
The output would look like
Var1 Var2
1 o o
2 Var1 o
3 o Var2
4 Var1 Var2
5 o o
6 Var1 o
7 o Var2
8 Var1 Var2
CodePudding user response:
Do you need
df[,colMeans(df=="o")<1]
that is remove all columns which have all 'o's?
CodePudding user response:
Here are couple of options -
- Base R
Filter
.
res <- Filter(function(x) any(x != 'o'), df)
res
# Var1 Var2
#1 o o
#2 Var1 o
#3 o Var2
#4 Var1 Var2
#5 o o
#6 Var1 o
#7 o Var2
#8 Var1 Var2
- Using
purrr
skeep
anddiscard
-
purrr::keep(df, ~any(.x != 'o'))
purrr::discard(df, ~all(.x == 'o'))