2017-05-03 52 views
0

行值我有這樣的數據集獲取對應於列名

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)) 
+1

除了答案帖子之外,你應該注意到你正面臨着這個問題,因爲數據不整齊 - 最好轉換成長格式的imo。 –

+0

@docendodiscimus這只是數據的一個子集,它也有其他變量。變量'to_select'是由於變異係數最小的值的均值而創建的。我認爲以長格式處理數據會很困難。 – Keniajin

回答

2

隨着基R,安全策略會是這樣的

cols <- as.character(unique(Book2$to_select)) 
row_col <- match(Book2$to_select, cols) 
idx <- cbind(seq_along(Book2$to_select), row_col) 
selected <- Book2[, cols][idx] 

Book2$selected <- selected 

或者用tidyverse包,像

library(tidyverse) 
Book2 %>% mutate(row=1:n()) %>% 
    gather(prop, val, meanX3:meanX2) %>% 
    group_by(row) %>% 
    mutate(selected=val[to_select==prop]) %>% 
    spread(prop, val) %>% select(-row) 

將是一個不錯的策略。

2

一種方式是按使用rowwise()然後get在「to_select」的字符串值列

Book2 %>% 
    rowwise() %>% 
    mutate(selected = get(as.character(to_select))) 
# A tibble: 5 × 5 
#  meanX3 meanX1 meanX2 to_select selected 
#  <dbl> <dbl> <dbl> <fctr>  <dbl> 
#1 21.66667 23.0 24.5 meanX3 21.66667 
#2 21.66667 34.5 26.5 meanX1 34.50000 
#3 11.00000 10.0 20.0 meanX2 20.00000 
#4 25.00000 25.0 25.0 meanX1 25.00000 
#5 240.33333 233.5 246.5 meanX1 233.50000 
+0

它拋出一個錯誤'錯誤:對象'meanX3'找不到' – Keniajin

+0

@Keniajin你的dplyr版本是什麼,因爲我沒有得到錯誤根據你發佈的例子 – akrun

+0

我正在使用'dplyr_0.5.0' – Keniajin

2

排在基礎R可以使用match選擇所需的列,然後基質子集選擇特定元件的每一行像這樣

Book2$selected <- as.numeric(Book2[cbind(seq_len(nrow(Book2)), 
              match(Book2$to_select, names(Book2)))])