Having this kind of data (see below), I try to create a new column IRISCOM
based on a conditional test with mutate().
COMMUNE|IRIS|NB
35360|353600101|45
35361|ZZZZZZ|72
I have tried with data=mutate(data, IRISCOM= ifelse(IRIS == "ZZZZZZZZZ", COMMUNE, IRIS))
It writes correctly COMMUNE but does write 1
in place of IRIS code as expected.
I've tried some more experimentations with no success.
To fix the factor
issue (see my comment above) we can do the following
library(tidyverse)
data %>%
mutate_if(is.factor, as.character) %>%
mutate(IRISCOM = ifelse(IRIS == "ZZZZZZ", COMMUNE, IRIS))
# COMMUNE IRIS NB IRISCOM
#1 35360 353600101 45 353600101
#2 35361 ZZZZZZ 72 35361
data <- read.table(text =
"COMMUNE|IRIS|NB
35360|353600101|45
35361|ZZZZZZ|72", header = T, sep = "|")
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.