行值我有這樣的數據集獲取對應於列名
Book2 <- structure(list(meanX3 = c(21.66666667, 21.66666667, 11, 25, 240.3333333
), meanX1 = c(23, 34.5, 10, 25, 233.5), meanX2 = c(24.5, 26.5,
20, 25, 246.5), to_select = structure(c(3L, 1L, 2L, 1L, 1L), .Label = c("meanX1",
"meanX2", "meanX3"), class = "factor"), selected = c(NA, NA,
NA, NA, NA)), .Names = c("meanX3", "meanX1", "meanX2", "to_select",
"selected"), class = "data.frame", row.names = c(NA, -5L))
我希望得到有關變量to_select
列名的coresponding行值。
我已經試過
Book2 %>% dplyr::mutate(selected=.[paste0(to_select)])
但它返回所有列的值。我該如何去得到一個數據集的像
structure(list(meanX3 = c(21.66666667, 21.66666667, 11, 25, 240.3333333
), meanX1 = c(23, 34.5, 10, 25, 233.5), meanX2 = c(24.5, 26.5,
20, 25, 246.5), to_select = structure(c(3L, 1L, 2L, 1L, 1L), .Label = c("meanX1",
"meanX2", "meanX3"), class = "factor"), selected = c(21.66, 34.5,
20, 25, 240.33)), .Names = c("meanX3", "meanX1", "meanX2", "to_select",
"selected"), class = "data.frame", row.names = c(NA, -5L))
除了答案帖子之外,你應該注意到你正面臨着這個問題,因爲數據不整齊 - 最好轉換成長格式的imo。 –
@docendodiscimus這只是數據的一個子集,它也有其他變量。變量'to_select'是由於變異係數最小的值的均值而創建的。我認爲以長格式處理數據會很困難。 – Keniajin