【问题标题】:How can I assign a name in the column if the name is found in another column?如果在另一列中找到名称,如何在该列中分配名称?
【发布时间】:2021-09-16 21:50:25
【问题描述】:

我有一个如下所示的数据框:


df = structure(list(Date = structure(c(16437, 16437, 16445, 16448, 
16450, 16451, 16451, 16460, 16461, 16464, 16466, 16466, 16468, 
16471, 16478), class = "Date"), Title = c("Interview with Handelsblatt", 
"Stability and Prosperity in Monetary Union", "Interview avec France 24", 
"Interview with Die Welt", "Interview with Die Zeit", "Interview with Libération", 
"Interview with the Irish Times", "Advancing Monetary Union", 
"Interview with Europe 1", "Interview with Corriere della Sera", 
"Monetary policy challenges in the euro area", "Interview with Süddeutsche Zeitung", 
"Lamfalussy was right: independence and interdependence in a monetary union", 
"Interview with Les Echos", "Economic Developments in the Euro Area"
)), row.names = c(NA, 15L), class = "data.frame")


         Date                                                                      Title
1  2015-01-02                                                Interview with Handelsblatt
2  2015-01-02                                 Stability and Prosperity in Monetary Union
3  2015-01-10                                                   Interview avec France 24
4  2015-01-13                                                    Interview with Die Welt
5  2015-01-15                                                    Interview with Die Zeit
6  2015-01-16                                                  Interview with Libération
7  2015-01-16                                             Interview with the Irish Times
8  2015-01-25                                                   Advancing Monetary Union
9  2015-01-26                                                    Interview with Europe 1
10 2015-01-29                                         Interview with Corriere della Sera
11 2015-01-31                                Monetary policy challenges in the euro area
12 2015-01-31                                         Interview with Süddeutsche Zeitung
13 2015-02-02 Lamfalussy was right: independence and interdependence in a monetary union
14 2015-02-05                                                   Interview with Les Echos
15 2015-02-12                                     Economic Developments in the Euro Area

我想要做的是创建一个附加列(“类型”),如果采访在“标题”列中,则其行名称为“采访”,否则为 NA。结果应如下所示:

         Date                                                                      Title
1  2015-01-02                                                Interview with Handelsblatt
2  2015-01-02                                 Stability and Prosperity in Monetary Union
3  2015-01-10                                                   Interview avec France 24
4  2015-01-13                                                    Interview with Die Welt
5  2015-01-15                                                    Interview with Die Zeit
6  2015-01-16                                                  Interview with Libération
7  2015-01-16                                             Interview with the Irish Times
8  2015-01-25                                                   Advancing Monetary Union
9  2015-01-26                                                    Interview with Europe 1
10 2015-01-29                                         Interview with Corriere della Sera
11 2015-01-31                                Monetary policy challenges in the euro area
12 2015-01-31                                         Interview with Süddeutsche Zeitung
13 2015-02-02 Lamfalussy was right: independence and interdependence in a monetary union
14 2015-02-05                                                   Interview with Les Echos
15 2015-02-12                                     Economic Developments in the Euro Area

Type 

Interview
NA
Interview
Interview
Interview
Interview
Interview
NA
Interview
Interview
NA
Interview
NA
Interview
NA



我正在尝试使用循环和 if 语句,但它变得相当复杂,我无法得到我想要的。

谁能帮帮我?

谢谢!

【问题讨论】:

  • str_extract(Title, "[Ii]nterview")?

标签: r dataframe loops for-loop if-statement


【解决方案1】:

怎么样

df$Type = NA
df$Type[grepl(pattern = 'Interview',
              x = df$Title)] = 'Interview'

【讨论】:

  • 重复答案(holzben 比你领先一分钟 ;-))
  • 非常感谢您的所有回答!我将在我拥有的真实数据框中让我的生活更轻松的那个标记为“正确”。再次感谢!
【解决方案2】:

您可以像这样使用ifelse


df$Type <- ifelse(grepl("Interview", df$Title), "Interview", NA_character_)

df
#>          Date
#> 1  2015-01-02
#> 2  2015-01-02
#> 3  2015-01-10
#> 4  2015-01-13
#> 5  2015-01-15
#> 6  2015-01-16
#> 7  2015-01-16
#> 8  2015-01-25
#> 9  2015-01-26
#> 10 2015-01-29
#> 11 2015-01-31
#> 12 2015-01-31
#> 13 2015-02-02
#> 14 2015-02-05
#> 15 2015-02-12
#>                                                                         Title
#> 1                                                 Interview with Handelsblatt
#> 2                                  Stability and Prosperity in Monetary Union
#> 3                                                    Interview avec France 24
#> 4                                                     Interview with Die Welt
#> 5                                                     Interview with Die Zeit
#> 6                                                   Interview with Libération
#> 7                                              Interview with the Irish Times
#> 8                                                    Advancing Monetary Union
#> 9                                                     Interview with Europe 1
#> 10                                         Interview with Corriere della Sera
#> 11                                Monetary policy challenges in the euro area
#> 12                                         Interview with Süddeutsche Zeitung
#> 13 Lamfalussy was right: independence and interdependence in a monetary union
#> 14                                                   Interview with Les Echos
#> 15                                     Economic Developments in the Euro Area
#>         Type
#> 1  Interview
#> 2       <NA>
#> 3  Interview
#> 4  Interview
#> 5  Interview
#> 6  Interview
#> 7  Interview
#> 8       <NA>
#> 9  Interview
#> 10 Interview
#> 11      <NA>
#> 12 Interview
#> 13      <NA>
#> 14 Interview
#> 15      <NA>

【讨论】:

    【解决方案3】:

    你可以这样做:

    # Assign new column, all NA
    df$type <- NA
    
    # override rows where title starts with 'Interview'
    df$type[grepl("^Interview", df$Title)] <- "Interview"
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-10-02
      • 1970-01-01
      • 2013-08-02
      相关资源
      最近更新 更多