【发布时间】: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