這一個已被竊聽我幾天,現在,我還沒有對堆棧交換任何運氣呢。實質上,我有兩個表,一個表定義從第二個表中選擇哪些列(按列編號)。我最初的計劃是串起來的列並傳遞到子選擇語句,但是當我定義字符串作爲as.character它不開心,即:子集的數據幀由特定的列可變數目R
# Data Sets, Variable_Selection: Table of Columns to Select from Variable_Table
VARIABLE_SELECTION <- data.frame(Set.1 = c(3,1,1,1,1), Set.2 = c(0,3,2,2,2), Set.3 = c(0,0,3,4,3),
Set.4 = c(0,0,0,5,4), Set.5 = c(0,0,0,0,5))
VARIABLE_TABLE <- data.frame(Var.1 = runif(100,0,10), Var.2 = runif(100,-100,100), Var.3 = runif(100,0,1),
Var.4 = runif(100,-1000,1000), Var.5 = runif(100,-1,1), Var.6 = runif(100,-10,10))
# Sting rows into character string of columns to select
VARIABLE_STRING <- apply(VARIABLE_SELECTION,1,paste,sep = ",",collapse = " ")
VARIABLE_STRING <- gsub(" ",",",VARIABLE_STRING)
VARIABLE_STRING <- data.frame(VAR_STRING = gsub(",0","",VARIABLE_STRING))
# Will actually be part of lapply function but, one line selection for demonstration:
VARIABLE_SINGLE_SET <- as.character(VARIABLE_STRING[4,])
# Subset table for selected columns
VARIABLE_TABLE_SUB_SELECT <- VARIABLE_TABLE[,c(VARIABLE_SINGLE_SET)]
# Error Returned:
# Error in `[.data.frame`(VARIABLE_TABLE, , c(VARIABLE_SINGLE_SET)) :
# undefined columns selected
我知道文本格式的問題,但我找不到解決方法,有什麼建議?
大!!!!非常感謝,我認爲它會是這樣的。它的作品,乾杯。 – user3322865