To achieve what you want you have several options.
Using subset
When calling subset
, you do not need to specify with the dollar sign ( $
) the table and the column you are comparing. The names you use will be searched within the table you give as the x
argument.
So, you can do the following:
subset(x = tabla_comp,
subset = Rfcms.RF.nearestCMS == Rfcms.RF.predictedCMS &
Rfcms.RF.predictedCMS == SScms.SSP.nearestCMS &
SScmp.SSP.nearestCMS == SScms.SSP.predictedCMS)
Using brackets
The above can be applied to notation with brackets ( []
)
tabla_comp[tabla_comp$Rfcms.RF.nearestCMS == tabla_comp$Rfcms.RF.predictedCMS &
tabla_comp$Rfcms.RF.predictedCMS == tabla_comp$SScms.SSP.nearestCMS &
tabla_comp$SScmp.SSP.nearestCMS == tabla_comp$SScms.SSP.predictedCMS, ]
Using brackets and functions
The previous methods have the disadvantage that they require many times to write values with long and complete names, which can cause "finger" errors to be commented and with this we do not obtain the expected result.
In addition to that it needs to be rewritten in case we have a table with different column names or with more or less columns. This makes our code more difficult to maintain.
Therefore, it is desirable to use the capabilities of R. We combine the bracket notation with some functions.
mi_df[which(apply(X = mi_df, MARGIN = 1, FUN = function(renglon) { length(unique(renglon)) } ) == 1), ]
We use apply
with an anonymous function.
First we obtain all the unique values that each row of our table has using unique
and once this is done, we use length
to count how many unique values each line has. If the result of this is number 1, then all values are equal in all columns.
We use which
within the brackets to select only the lines where True ( TRUE
) that the result of calling this function is == 1
.
This procedure can be reused no matter how "high" or "wide" the table in question is, as long as it has the same type of data as the table you have shown in this question.