0

I have a dataframe in the following form:

 Date     equity company  press Categorization  Year Month Event greenwashing
  <chr>     <dbl> <chr>    <chr> <chr>          <dbl> <chr> <dbl> <chr>       
1 07/30/21   153. JPMorgan NA    NA                NA NA       NA 0           
2 07/29/21   153  JPMorgan NA    NA                NA NA       NA 0           
3 07/28/21   152. JPMorgan NA    NA                NA NA       NA 0           
4 07/27/21   151. JPMorgan NA    NA                NA NA       NA 0           
5 07/26/21   152. JPMorgan NA    NA                NA NA       NA 0           
6 07/23/21   151. JPMorgan NA    NA                NA NA       NA 0

In the column 'greenwashing' there are some variables that are in the format character such as: The Guardian, Financial Times, among others. I need to turn these characters into 1.

I already tried to name a word list and use the if else code:

word.list = c("Financial Times",
              "The Guardian", 
              "Mena Report",
              "States News Service",
              "US Newshire",
              "DeSmogBlog",
              "PR Newshire",
              "The New York Times")
if(word.list){
  print("1")
zork1
  • 69
  • 5

3 Answers3

2

We can do this easily by converting the logical to integer with +

df$greenwashing <- +(df$greenwashing %in% word.list)
akrun
  • 874,273
  • 37
  • 540
  • 662
1

Using tidyverse: Let's say your data frame is df, working off your attempt

library(tidyverse)

df <- mutate(df, greenwashing = ifelse(greenwashing %in% word.list, 1, greenwashing)

GVK
  • 103
  • 7
1

You can try -

df$greenwashing <- as.integer(df$greenwashing %in% word.list)

This will change all the word.list values to 1 and rest to 0

Ronak Shah
  • 377,200
  • 20
  • 156
  • 213