ritm
July 20, 2020, 9:01am
1
I have a dataframe in R which has two columns Machine Name(character) and Region(factor).They have values as follows(few examples):
Machine Name Region
1233.corp.pdo.om APAC
xyz.om Europe
345.corp.pdo.cm Europe
abc12.cm Americas
So i want to update the region with pdo for the machines which have corp.pdo in them.But when i am trying to update its not happening using if else.Can you please help.
Something like this?
library(dplyr, warn.conflicts = FALSE)
library(stringr)
df <- tribble(~ `Machine Name`, ~ Region,
"1233.corp.pdo.om", "APAC",
"xyz.om", "Europe",
"345.corp.pdo.cm", "Europe",
"abc12.cm", "Americas")
mutate(df, Region = if_else(str_detect(`Machine Name`, "corp.pdo"), "pdo", Region))
#> # A tibble: 4 x 2
#> `Machine Name` Region
#> <chr> <chr>
#> 1 1233.corp.pdo.om pdo
#> 2 xyz.om Europe
#> 3 345.corp.pdo.cm pdo
#> 4 abc12.cm Americas
Created on 2020-07-20 by the reprex package (v0.3.0)
ritm
July 20, 2020, 9:48am
3
Yes like this only.Let me try this.Hope this works.I will update you.Thanks.
ritm
July 20, 2020, 9:56am
4
siddharthprabhu:
mutate(df, Region = if_else(str_detect(
Machine Name, "corp.pdo"), "pdo", Region))
I get the following error:
Error: false
must be a character vector, not a factor
object
Its got to do with Region column i guess which is a factor type.
Indeed. I missed the part where you stated that Region
is a factor. The simplest workaround is to coerce it into a character vector. You can always convert it back to factor later with pdo added as a new level if necessary.
df %>%
mutate(Region = as.factor(Region)) %>%
mutate(Region = if_else(str_detect(`Machine Name`, "corp.pdo"), "pdo", as.character(Region)))
#> # A tibble: 4 x 2
#> `Machine Name` Region
#> <chr> <chr>
#> 1 1233.corp.pdo.om pdo
#> 2 xyz.om Europe
#> 3 345.corp.pdo.cm pdo
#> 4 abc12.cm Americas
ritm
July 20, 2020, 10:57am
6
I tried doing that.Although it doesn't throw any error but it doesn't update the values for those records.
Code:-
df3_machine_region %>%mutate(Region= ifelse(str_detect(Machine.Name, "corp.pdo"), "pdo", as.character(Region)))
Output:(sample)
Machine Name Region
D120710.corp.pdo.om
Europe
D120867.corp.pdo.om
Americas
Not sure about that. It would help if you could post a self-contained reproducible example with a subset of your data.
A minimal reproducible example consists of the following items:
A minimal dataset, necessary to reproduce the issue
The minimal runnable code necessary to reproduce the issue, which can be run
on the given dataset, and including the necessary information on the used packages.
Let's quickly go over each one of these with examples:
Minimal Dataset (Sample Data)
You need to provide a data frame that is small enough to be (reasonably) pasted on a post, but big enough to reproduce your issue.
Let's say, as an example, that you are working with the iris data frame
head(iris)
#> Sepal.Length Sepal.Width Petal.Length Petal.Width Species
#> 1 5.1 3.5 1.4 0.…
system
Closed
August 10, 2020, 11:07am
8
This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.