У меня есть код ниже, чтобы искать строки, которые имеют одинаковое значение в кадре данных.
Однако его не следует запускать, если в фрейме данных содержится только 1 столбец данных.
Использование его только в 1 столбце данных исключает данные, потому что все уникально.
Я пытался включить оператор if, который сначала проверяет количество столбцов. Но я сталкиваюсь с ошибкой ниже.
#Dataframe with only 1 column of data
final_table <- as.data.frame(c("a","b","c","d","e"))
#Remove rows where all values are the same if dataframe ncol >1.
final_table <- final_table %>%
if(ncol(final_table) > 1)
{filter(apply(., 1, function(x) length(unique(x)) > 1))}
#Error in if (.) ncol(final_table) > 1 else { : argument is not interpretable as logical
df1 <- data.frame("x1" = c("a","b","c","d","e"),
"x2" = c("c","b","c","x","e"),
"x3" = c("a","b","t","s","e"))
df1 %>%
filter(apply(., 1, function(x) length(unique(x)) > 1 ))
#df1 before when run on multiple columns
x1 x2 x3
1 a c a
2 b b b
3 c c t
4 d x s
5 e e e
#after when run on multiple columns (correct results)
x1 x2 x3
1 a c a
2 c c t
3 d x s
#before if run on df with 1 column
1 a
2 b
3 c
4 d
5 e
#after if run on df with 1 column (results not correct)
#I need to insert a conditional statement that checks if ncol > 1
#If ncol == 1 then I don't want to run the function
NA