2017-09-25 48 views
0

我在爲r的下列數據幀如何從r數據幀中提取細胞,並添加爲新行

Names  X_1   X_2   X_3   X_4 
Name   Sagar II Booster 
Location  India  No   Discharge  Open 
Depth  19.5  start   End 
DOC   3.2   FPL   64 
Qunatity  234   SPL   50 

現在我想提取某些細胞並在下一單元格及其對應的值。 我想要的數據幀將

Names  Values 
Name   Sagar II 
Location  India 
Discharge Open 
Depth  19.5 
DOC   3.2 
FPL   64 
SPL   50 

我該怎麼辦呢r中?

+0

提取的名字應該在數據框的文本完全匹配。 'C(名稱,位置,排放,深度,DOC,FPL,SPL)' – Neil

+0

所以給定列可以同時擁有_key_和_value_? – useR

+0

鑑於列將有'key'和列旁邊將有'value'請參考我想要的數據幀 – Neil

回答

1

一個從基礎解決方案R.

# Create example data frame 
dt <- read.table(text = "Names  X_1   X_2   X_3   X_4 
Name   Sagar II Booster 
       Location  India  No   Discharge  Open 
       Depth  19.5  start   End 
       DOC   3.2   FPL   64 
       Qunatity  234   SPL   50", 
       stringsAsFactors = FALSE, header = TRUE, fill = TRUE) 

# A list of target keys 
target_key <- c("Name", "Location", "Discharge", "Depth", "DOC", "FPL", "SPL") 

# A function to extract value based on key and create a new data frame 
extract_fun <- function(key, df = dt){ 
    Row <- which(apply(dt, 1, function(x) key %in% x)) 
    Col <- which(apply(dt, 2, function(x) key %in% x)) 
    df2 <- data.frame(Names = key, Values = df[Row, Col + 1], 
        stringsAsFactors = FALSE) 
    df2$Values <- as.character(df2$Values) 
    return(df2) 
} 

# Apply the extract_fun 
ext_list <- lapply(target_key, extract_fun) 

# Combine all data frame 
dt_final <- do.call(rbind, ext_list) 

dt_final 
     Names Values 
1  Name Sagar 
2 Location India 
3 Discharge Open 
4  Depth 19.5 
5  DOC 3.2 
6  FPL  64 
7  SPL  50 
1

可能不是最有效的,但是適用於你的例子:

library(dplyr) 

key_value = function(extraction){ 
    temp = matrix(NA, nrow = length(extraction), ncol = 2) 
    temp[,1] = extraction 

    for(ii in 1:nrow(temp)){ 
    index = df %>% 
     as.matrix %>% 
     {which(. == extraction[ii], arr.ind = TRUE)} 

    temp[ii, 2] = index %>% {df[.[1], .[2]+1]} 
    } 

    return(data.frame(Names = temp[,1], Values = temp[,2])) 
} 

結果:

> vec = c("Name", "Location", "Discharge", "Depth", "DOC", "FPL", "SPL") 

> key_value(vec) 
     Names Values 
1  Name SagarII 
2 Location India 
3 Discharge Open 
4  Depth 19.5 
5  DOC  3.2 
6  FPL  64 
7  SPL  50 

數據:

df = read.table(text = "Names  X_1   X_2   X_3   X_4 
       Name   SagarII Booster NA NA 
       Location  India  No   Discharge  Open 
       Depth  19.5  start   End NA 
       DOC   3.2   FPL   64 NA 
       Qunatity  234   SPL   50 NA", header = TRUE, stringsAsFactors = FALSE)