如何从r数据帧中提取单元格并添加为新行

时间:2017-09-25 12:49:40

标签: r dataframe

我在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?

中这样做

2 个答案:

答案 0 :(得分: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)

答案 1 :(得分: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