From a data frame in this structure
1 2 3
0.2393876 0.3388785 0.4217339
0.2984463 0.3948705 0.3066832
0.3042033 0.3321453 0.3636514
0.2290906 0.2796392 0.4912702
0.1971198 0.3273682 0.4755120
0.2328518 0.3991225 0.3680256
0.3031909 0.3790213 0.3177878
0.2734835 0.3233044 0.4032121
0.3190916 0.3256503 0.3552581
0.2600000 0.3281130 0.4118871
How is it possible to compare the value in the three columns, find the highest and add the name of the column in a new variable. Example output:
1 2 3 highestcolumn
0.2393876 0.3388785 0.4217339 3
0.2984463 0.3948705 0.3066832 2
0.3042033 0.3321453 0.3636514 3
CodePudding user response:
Use which.max
and apply
on each row.
df$highestcolumn <- apply(df, 1, which.max)
> df
X1 X2 X3 mean
1 0.2393876 0.3388785 0.4217339 3
2 0.2984463 0.3948705 0.3066832 2
3 0.3042033 0.3321453 0.3636514 3
4 0.2290906 0.2796392 0.4912702 3
5 0.1971198 0.3273682 0.4755120 3
6 0.2328518 0.3991225 0.3680256 2
7 0.3031909 0.3790213 0.3177878 2
8 0.2734835 0.3233044 0.4032121 3
9 0.3190916 0.3256503 0.3552581 3
10 0.2600000 0.3281130 0.4118871 3
CodePudding user response:
We may use max.col
df$highestcolumn <- max.col(df, "first")
-output
> df
1 2 3 highestcolumn
1 0.2393876 0.3388785 0.4217339 3
2 0.2984463 0.3948705 0.3066832 2
3 0.3042033 0.3321453 0.3636514 3
4 0.2290906 0.2796392 0.4912702 3
5 0.1971198 0.3273682 0.4755120 3
6 0.2328518 0.3991225 0.3680256 2
7 0.3031909 0.3790213 0.3177878 2
8 0.2734835 0.3233044 0.4032121 3
9 0.3190916 0.3256503 0.3552581 3
10 0.2600000 0.3281130 0.4118871 3