首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如果在另一列中找到了名称,我如何在该列中分配名称?

如果在另一列中找到了名称,我如何在该列中分配名称?
EN

Stack Overflow用户
提问于 2021-07-06 17:04:59
回答 3查看 28关注 0票数 0

我有一个数据帧,如下所示:

代码语言:javascript
复制
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

我想要做的是创建一个额外的列("Type"),如果interview在"Title“列中,它的行名为"interview”,否则为NA。结果应该如下所示:

代码语言:javascript
复制
         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语句,但它变得非常复杂,我无法得到我想要的东西。

有谁可以帮我?

谢谢!

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2021-07-06 17:10:49

您可以像这样使用ifelse

代码语言:javascript
复制
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>
票数 1
EN

Stack Overflow用户

发布于 2021-07-06 17:10:14

你可以这样做:

代码语言:javascript
复制
# Assign new column, all NA
df$type <- NA

# override rows where title starts with 'Interview'
df$type[grepl("^Interview", df$Title)] <- "Interview"
票数 1
EN

Stack Overflow用户

发布于 2021-07-06 17:11:18

怎么样

代码语言:javascript
复制
df$Type = NA
df$Type[grepl(pattern = 'Interview',
              x = df$Title)] = 'Interview'
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68267572

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档