【问题标题】:Loop over data frame rows and create data frame from new rows循环数据框行并从新行创建数据框
【发布时间】:2017-10-10 10:45:41
【问题描述】:

我正在尝试遍历 DF 的行。在循环遍历行时,我想对某些行进行一些更改,并创建一个包含新行的新数据框。

我使用的数据框看起来可以用这个来创建:

df <- structure(
  list(
    campaign_name = c(
      "Category> fanshop",
      "Category> trainingspakken",
      "Category> trainingsshirts",
      "Category> hoodies",
      "Category> broeken",
      "Category> voetbalshirts"
    ),
    ad_group = c(
      "Pro team X[B]",
      "Pro team X[B]",
      "Pro team X[B]",
      "Pro team X[B]",
      "Pro team X[B]",
      "Pro team X[B]"
    ),
    category = c(
      "fanshop",
      "trainingspakken",
      "trainingsshirts",
      "hoodies",
      "broeken",
      "voetbalshirts"
    ),
    Final_URL = c(
      "https://fanshop/Pro-team-X.html",
      "https://fanshop/Pro-team-X/_Trainingspakken",
      "https://fanshop/Pro-team-X/_Trainingsshirts",
      "https://fanshop/Pro-team-X/_Hoodies_Sweaters",
      "https://fanshop/Pro-team-X/_Korte-broeken_Lange-broeken",
      "https://fanshop/Pro-team-X/_Voetbalshirts"
    ),
    team_name = c(
      "Pro team X",
      "Pro team X",
      "Pro team X",
      "Pro team X",
      "Pro team X",
      "Pro team X"
    ),
    keyword = c(
      "+Pro +team +X +fanshop",
      "+Pro +team +X +trainingspakken",
      "+Pro +team +X +trainingsshirts",
      "+Pro +team +X +hoodies",
      "+Pro +team +X +broeken",
      "+Pro +team +X +voetbalshirts"
    ),
    Criterion_type = c("Broad", "Broad", "Broad", "Broad", "Broad", "Broad")
  ),
  .Names = c(
    "campaign_name",
    "ad_group",
    "category",
    "Final_URL",
    "team_name",
    "keyword",
    "Criterion_type"
  ),
  row.names = c("1", "2", "3", "4", "5", "6"),
  class = "data.frame"
)

如果我使用下面的功能,行打印得很好并且被改变了。但是一旦我尝试将它分配给数据框,当然每次循环运行时它都会被覆盖。

for ( row in 1:nrow(df)) {
  temp_row <- df[row,]
  if (temp_row$Criterion_type == "Broad") {
    temp_row$keyword <- gsub("\\+", "", temp_row$keyword)
    temp_row$Criterion_type <- "Negative Exact"
  }
  print(temp_row)
}

在这里查看了许多问题并尝试了许多方法后,我仍然无法正确完成它。非常感谢!

我希望根据上面的 IF 语句修改每一行。 1 行如下所示:

campaign_name   ad_group    category    Final_URL   team_name   keyword Criterion_type
Category> voetbalshirts Pro team X[B]   voetbalshirts   https://fanshop/Pro-team-X/_Voetbalshirts   Pro team X  paris saint germain voetbalshirts   Negative Exact

我已经尝试过的一些问题:

How to append rows to an R data frame

duplicate rows and create new data frame in R

【问题讨论】:

  • @coffeinjunky 谢谢,对不起。通常我总是接受。不知怎的,我现在忘记了。

标签: r for-loop dataframe rbind


【解决方案1】:

试试这个:

library(dplyr)
new_df <- data.frame()
for ( row in 1:nrow(df)) {
  temp_row <- df[row,]
  if (temp_row$Criterion_type == "Broad") {
    new_df <- bind_rows(new_df, data.frame(keyword=gsub("\\+", "", temp_row$keyword), Criterion_type = "Negative Exact"))
  }
}

这为您提供了一个新的数据框,如下所示:

new_df

                              keyword Criterion_type
1         paris saint germain fanshop Negative Exact
2 paris saint germain trainingspakken Negative Exact
3 paris saint germain trainingsshirts Negative Exact
4         paris saint germain hoodies Negative Exact
5         paris saint germain broeken Negative Exact
6   paris saint germain voetbalshirts Negative Exact

不过,请注意,您可以更轻松地实现这一目标,并且可能更快(因为矢量化了)。例如,

df$keyword <- with(df, 
                   ifelse(Criterion_type=="Broad", gsub("\\+", "", keyword), keyword))
df$Criterion_type <- with(df, 
                   ifelse(Criterion_type=="Broad", "Negative Exact", Criterion_type))

实现了相同的效果并且更具可读性。

【讨论】:

  • 谢谢!愚蠢的我。我在 for 循环中创建了 DF,而我之前应该这样做。完全忘记了。
猜你喜欢
  • 1970-01-01
  • 2018-06-25
  • 1970-01-01
  • 2013-08-17
  • 1970-01-01
  • 2020-03-29
  • 1970-01-01
  • 2017-06-01
  • 2021-09-28
相关资源
最近更新 更多