mardi 6 juillet 2021

How can I assign a name in the column if the name is found in another column?

I have a dataframe like the one below:


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

What I want to do is creating an additional column ("Type") whose rows take the name "interview" if interview is in the column "Title" and NA otherwise. The result should be like the one below:

         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



I am trying with loops and if statements but it becomes quite complicated and I can't get what I want.

Can anyone help me?

Thanks!

Aucun commentaire:

Enregistrer un commentaire