Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 201867

Taking a value from another column if column value == "character(0)"

$
0
0

In the following data:

library(stringr)
library(purrr)
entry <- c("P17-Nationalist Revolutionary Movement-Free Bolivia Movement (Movimiento Nacionalista Revolucionario [MNR] - Movimiento Bolivia Libre [MBL] [MNR-MBL])",
           "P18-Socialist Party (Partido Socialista [PS])",
           "P19-Liberty and Justice (Libertad y Justicia [LJ])",
           "")
something <- c(1,2,3,4)
x <- str_match_all(entry, "(?<=\\[).+?(?=\\])") %>% map(drop)
Election_Parties <- tibble::tibble(x, something, entry)

I would like to replace the character(0) value with the value from a different column like this:

setDT(Election_Parties)[identical(x, character(0)) ,x := entry]

This however gives me the error:

Error in `[.data.table`(setDT(Election_Parties), identical(Election_Parties[,  : 
  LHS of := must be a symbol, or an atomic vector (column names or positions).

What am I doing wrong?

DESIRED OUTPUT:

library(stringr)
library(purrr)
entry <- c("P17-Nationalist Revolutionary Movement-Free Bolivia Movement (Movimiento Nacionalista Revolucionario [MNR] - Movimiento Bolivia Libre [MBL] [MNR-MBL])",
           "P18-Socialist Party (Partido Socialista [PS])",
           "P19-Liberty and Justice (Libertad y Justicia [LJ])",
           "4")
something <- c(1,2,3,4)
x <- str_match_all(entry, "(?<=\\[).+?(?=\\])") %>% map(drop)
Election_Parties <- tibble::tibble(x, something, entry)

Viewing all articles
Browse latest Browse all 201867

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>