Assume we have the following melted data :
melted=structure(list(Var1 = c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 1L, 2L,
3L, 4L, 5L, 6L, 7L, 8L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 1L, 2L,
3L, 4L, 5L, 6L, 7L, 8L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 1L, 2L,
3L, 4L, 5L, 6L, 7L, 8L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 1L, 2L,
3L, 4L, 5L, 6L, 7L, 8L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 1L, 2L,
3L, 4L, 5L, 6L, 7L, 8L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L), variable = structure(c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 5L,
5L, 5L, 5L, 5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 7L,
7L, 7L, 7L, 7L, 7L, 7L, 7L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 9L,
9L, 9L, 9L, 9L, 9L, 9L, 9L, 10L, 10L, 10L, 10L, 10L, 10L, 10L,
10L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L), .Label = c("Sensitivity",
"Specificity", "Pos Pred Value", "Neg Pred Value", "Precision",
"Recall", "F1", "Prevalence", "Detection Rate", "Detection Prevalence",
"Balanced Accuracy "), class = "factor"), value = c(0, 0.701244813278008,
0.701244813278008, 0.705394190871369, 0.711297071129707, 0.711297071129707,
0.7125, 0.714876033057851, 0, 0.295454545454545, 0.295454545454545,
0.318181818181818, 0.347826086956522, 0.347826086956522, 0.355555555555556,
0.372093023255814, 0, 0.845, 0.845, 0.85, 0.85, 0.85, 0.855,
0.865, 0, 0.152941176470588, 0.152941176470588, 0.164705882352941,
0.188235294117647, 0.188235294117647, 0.188235294117647, 0.188235294117647,
0, 0.845, 0.845, 0.85, 0.85, 0.85, 0.855, 0.865, 0, 0.701244813278008,
0.701244813278008, 0.705394190871369, 0.711297071129707, 0.711297071129707,
0.7125, 0.714876033057851, 0, 0.766439909297052, 0.766439909297052,
0.770975056689342, 0.774487471526196, 0.774487471526196, 0.777272727272727,
0.782805429864253, 0, 0.845614035087719, 0.845614035087719, 0.845614035087719,
0.83859649122807, 0.83859649122807, 0.842105263157895, 0.849122807017544,
0, 0.592982456140351, 0.592982456140351, 0.596491228070175, 0.596491228070175,
0.596491228070175, 0.6, 0.607017543859649, 0, 0.701754385964912,
0.701754385964912, 0.701754385964912, 0.701754385964912, 0.701754385964912,
0.701754385964912, 0.701754385964912, 0, 0.498349679366277, 0.498349679366277,
0.511788004526594, 0.529561579043114, 0.529561579043114, 0.534027777777778,
0.543484528156833)), class = "data.frame", row.names = c(NA,
-88L))
I need to add another column based in the following condition :
g=ifelse(melted[,"Var1"]==8,melted[,"variable"],NA)
However , i get numbers instead of the variable column in the dataset. For example :
print(g[1:16])
[1] NA NA NA NA NA NA NA 1 NA NA NA NA NA NA NA 2
But the output should be :
[1] NA NA NA NA NA NA NA Sensitivity NA NA NA NA NA NA NA Specificity.
To explain the pb in another way , Consider :
melted <- mutate(melted, Label = variable)
I need to replace Label with NA whenever Var1 !=8.
So again i tried :
melted <- mutate(melted, Label = ifelse(Var1!=8,NA,variable))
print(melted[1:16,])
Then i got :
Var1 variable value Label
1 1 Sensitivity 0.0000000 NA
2 2 Sensitivity 0.7012448 NA
3 3 Sensitivity 0.7012448 NA
4 4 Sensitivity 0.7053942 NA
5 5 Sensitivity 0.7112971 NA
6 6 Sensitivity 0.7112971 NA
7 7 Sensitivity 0.7125000 NA
8 8 Sensitivity 0.7148760 1
9 1 Specificity 0.0000000 NA
10 2 Specificity 0.2954545 NA
11 3 Specificity 0.2954545 NA
12 4 Specificity 0.3181818 NA
13 5 Specificity 0.3478261 NA
14 6 Specificity 0.3478261 NA
15 7 Specificity 0.3555556 NA
16 8 Specificity 0.3720930 2
However the expected result should be :
Var1 variable value Label
1 1 Sensitivity 0.0000000 NA
2 2 Sensitivity 0.7012448 NA
3 3 Sensitivity 0.7012448 NA
4 4 Sensitivity 0.7053942 NA
5 5 Sensitivity 0.7112971 NA
6 6 Sensitivity 0.7112971 NA
7 7 Sensitivity 0.7125000 NA
8 8 Sensitivity 0.7148760 Sensitivity
9 1 Specificity 0.0000000 NA
10 2 Specificity 0.2954545 NA
11 3 Specificity 0.2954545 NA
12 4 Specificity 0.3181818 NA
13 5 Specificity 0.3478261 NA
14 6 Specificity 0.3478261 NA
15 7 Specificity 0.3555556 NA
16 8 Specificity 0.3720930 Specificity