如何创建仅包含来自另一个数据框的某些行且具有列名的数据框?

时间:2019-06-14 08:19:38

标签: r dataframe rbind

我正在开发一个闪亮的R应用程序,需要在其中解析csv文件。从他们,我建立一个数据框。然后,我想从此数据框中提取一些行,并将其放入另一个数据框中。

我找到了一种使用rbind做到这一点的方法,但这很丑陋,而且似乎不够。

function(set){ #set is the data.frame containing the data I want to extract
  newTable <- data.frame(
    name = character(1),
    value = numeric(1),
    columnC = character(1),
    stringsAsFactors=FALSE)
  threshold <- 0

  for (i in 1:nrow(set)){

    value <- calculateValue(set$Value[[i]])) 
    if (value >= threshold){

      name <- set[which(set$Name == "foo")), ]$Name
      columnC <- set[which(set$C == "bar")), ]$C

      v <- c(name, value, columnC)

      newTable <- rbind(newTable, v)
    }
  }

如果我不使用character(1)numeric(1)初始化数据框值,则会收到错误消息:

  

警告:data.frame中的错误:参数暗示不同数量的   行:0、1 75:停止74:data.frame

但是这之后在数据框中留下了一行空白(字符为空字符串,数字为0)。


由于R是一种很酷的语言,所以我认为有一种更简单有效的方法。有人可以帮我吗?

1 个答案:

答案 0 :(得分:1)

除了遍历每一行之外,您还可以选择子集

function(set, threshold) {
    set[calculateValue(set$Value) >= threshold, c("name", "value", "columnC")]
}

或使用dplyr过滤行并选择列以获取所需的子集。

library(tidyverse)
function(set, threshold) {
    set %>%
    filter(calculateValue(Value) >= threshold) %>%
    select(name, value, columnC)
}

如果需要新的数据框,则将结果分配给新变量

getValueOverThreshold <- function(set, threshold) {
    set %>%
    filter(calculateValue(Value) >= threshold) %>%
    select(name, value, columnC)
}

newDF <- getValueOverThreshold(set, 0)

您可能想签出https://r4ds.had.co.nz/transform.html