循环数据框行并从新行

时间:2017-05-11 12:21:42

标签: r for-loop dataframe rbind

我试图循环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

1 个答案:

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

实现相同并且更具可读性。