我有一个数据帧,如下所示:
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。结果应该如下所示:
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语句,但它变得非常复杂,我无法得到我想要的东西。
有谁可以帮我?
谢谢!
发布于 2021-07-06 17:10:49
您可以像这样使用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>发布于 2021-07-06 17:10:14
你可以这样做:
# Assign new column, all NA
df$type <- NA
# override rows where title starts with 'Interview'
df$type[grepl("^Interview", df$Title)] <- "Interview"发布于 2021-07-06 17:11:18
怎么样
df$Type = NA
df$Type[grepl(pattern = 'Interview',
x = df$Title)] = 'Interview'https://stackoverflow.com/questions/68267572
复制相似问题