mutate - case_when - Rstudio

Asked

Viewed 97 times

0

I need to adjust one column to another. I have a column of hostpitais "HP" and a column of cities "City". I need to adjust the city according to the Hospital. Bwn is my database I’m making:

BWn = BWn %>% mutate(City = case_when(str_detect (HP, "H.U.DE BELLVITGE”) ~ ”Barcelona”, str_detect (HP, “H.G.U.GREGORIO MARAÑÓN”) ~ ” Madrid”, str_detect (HP, “H.CLÍNIC”) ~ ” Barcelona”, str_detect (HP, “H.U.QUIRÓN DEXEUS”) ~ ” Barcelona”, TRUE ~ “Sevilla”))

[![insert image description here][1][1]

But, when executing, it appears the "+", as if it had not finished... What am I wrong? There’s a simpler way to do?

I need hospitals to be related to cities equal to tabel: [! [insert image description here][2][2]

dput of data:

structure(list(ID = 1:10, HP = c("H.G.U.GREGORIO MARAÑÓN", "H.U.DE BELLVITGE",
"H.U.DE BELLVITGE", "H.CLÍNIC", "H.U.12 DE OCTUBRE", "H.G.U.GREGORIO MARAÑÓN",
"H.M.I. VIRGEN DE LAS NIEVES", "C.U.NAVARRA", "H.U.LA PAZ", "C.U.NAVARRA"
), City = c("Valencia", "Barcelona", "Madrid", "Barcelona", "Madrid",
"Madrid", "Sevilla", "Pamplona", "Madrid", "Pamplona"), Time = structure(c(81L,
25L, 162L, 143L, 265L, 179L, 252L, 190L, 221L, 63L), .Label = c("0",
"1,87", "10,04", "10,09", "10,15", "10,25", "10,32", "10,4",
"10,46", "10,5", "10,54", "10,56", "10,61", "10,63", "10,66",
"10,68", "10,74", "10,75", "10,77", "10,83", "10,88", "10,9",
"10,94", "10,99", "11", "11,01", "11,05", "11,07", "11,13", "11,27",
"11,34", "11,38", "11,39", "11,49", "11,51", "11,56", "11,59",
"11,73", "11,78", "11,8", "11,83", "11,92", "11,94", "11,99",
"12,02", "12,06", "12,11", "12,13", "12,22", "12,23", "12,27",
"12,29", "12,32", "12,43", "12,49", "12,58", "12,63", "12,65",
"12,67", "12,72", "12,74", "12,81", "12,87", "12,94", "12,95",
"12,97", "12,98", "13,06", "13,08", "13,11", "13,12", "13,14",
"13,17", "13,18", "13,21", "13,22", "13,26", "13,27", "13,29",
"13,3", "13,34", "13,39", "13,43", "13,53", "13,54", "13,57",
"13,58", "13,61", "13,71", "13,73", "13,75", "13,78", "13,79",
"13,8", "13,92", "14", "14,01", "14,11", "14,12", "14,26", "14,28",
"14,33", "14,34", "14,69", "14,71", "14,72", "14,8", "14,81",
"14,82", "15", "15,07", "15,28", "15,29", "15,4", "15,46", "15,54",
"15,56", "15,93", "15,97", "16,04", "16,05", "16,1", "16,15",
"16,17", "16,2", "16,31", "16,35", "16,38", "16,43", "16,53",
"16,59", "16,96", "17,17", "17,32", "17,77", "17,82", "17,84",
"18,06", "18,1", "18,31", "18,46", "18,48", "18,64", "18,89",
"19,34", "19,54", "19,71", "19,8", "2,6", "2,64", "20,57", "20,85",
"21,29", "21,4", "21,43", "21,85", "22,16", "23", "23,15", "23,56",
"23,72", "24", "3,26", "3,43", "3,66", "3,73", "3,99", "4", "4,06",
"4,21", "4,26", "4,6", "4,76", "4,81", "5,39", "5,58", "5,6",
"5,65", "5,68", "5,69", "5,85", "6,26", "6,27", "6,42", "6,56",
"6,63", "6,66", "6,72", "6,84", "6,86", "6,92", "7,02", "7,11",
"7,14", "7,24", "7,27", "7,32", "7,34", "7,39", "7,4", "7,45",
"7,58", "7,65", "7,69", "7,73", "7,75", "7,77", "7,82", "7,94",
"7,98", "8", "8,01", "8,02", "8,07", "8,24", "8,26", "8,36",
"8,38", "8,41", "8,42", "8,43", "8,48", "8,51", "8,53", "8,63",
"8,67", "8,69", "8,7", "8,74", "8,77", "8,8", "8,81", "8,82",
"8,86", "8,91", "8,93", "8,94", "8,95", "8,96", "8,98", "9,05",
"9,16", "9,17", "9,19", "9,29", "9,35", "9,36", "9,42", "9,44",
"9,53", "9,54", "9,55", "9,57", "9,58", "9,63", "9,65", "9,66",
"9,69", "9,74", "9,8", "9,83", "9,84", "9,85", "9,87", "9,89",
"9,94", "9,98"), class = "factor"), Day = c(5L, 1L, 2L, 4L, 3L,
6L, 3L, 5L, 3L, 5L), BW = structure(c(25L, 89L, 86L, 9L, 6L,
78L, 50L, 73L, 51L, 52L), .Label = c("1.45 kg", "1.65 kg",
"1.7 kg", "1.804 kg", "1025 gr", "1150 gr", "1173 gr", "1250 gr",
"1300 gr", "1435 gr", "1450 gr", "1599 gr", "1650 gr", "1700 gr",
"1722 gr", "1750 gr", "1800 gr", "1950 gr", "2 kg", "2,05 kg",
"2,9 kg", "2,95 kg", "2.05 kg", "2.1976 kg", "2.2 kg", "2.35 kg",
"2.419 kg", "2.5 kg", "2.55 kg", "2.6 kg", "2.68 kg", "2.7 kg",
"2.75 kg", "2.8 kg", "2.87 kg", "2.9 kg", "2.95 kg", "2000 gr",
"2009 gr", "2050 gr", "2100 gr", "2132 gr", "2200 gr", "2250 gr",
"2255 gr", "2350 gr", "2378 gr", "2450 gr", "2460 gr", "2500 gr",
"2550 gr", "2600 gr", "2650 gr", "2680 gr", "2700 gr", "2750 gr",
"2800 gr", "2850 gr", "2870 gr", "2900 gr", "2950 gr", "3 kg",
"3.1 kg", "3.15 kg", "3.25 kg", "3.4 kg", "3.5 kg", "3.55 kg",
"3.6 kg", "3.8 kg", "3000 gr", "3050 gr", "3100 gr", "3150 gr",
"3200 gr", "3250 gr", "3300 gr", "3400 gr", "3500 gr", "3550 gr",
"3600 gr", "3650 gr", "3680 gr", "3800 gr", "4 kg", "4.2 kg",
"4.85 kg", "4000 gr", "4100 gr", "4200 gr", "4850 gr"), class = "factor"),
BPD = structure(c(8L, 23L, 23L, 5L, 3L, 20L, 13L, 18L, 12L,
19L), .Label = c("100 mm", "64 mm", "68 mm", "72 mm", "74 mm",
"80 mm", "81 mm", "82 mm", "83 mm", "84 mm", "85 mm", "86 mm",
"87 mm", "88 mm", "89 mm", "90 mm", "91 mm", "92 mm", "93 mm",
"94 mm", "95 mm", "96 mm", "97 mm"), class = "factor"), AD = structure(c(31L,
20L, 21L, 22L, 26L, 11L, 6L, 10L, 7L, 3L), .Label = c("100 mm",
"101 mm", "102 mm", "103 mm", "104 mm", "105 mm", "106 mm",
"107 mm", "108 mm", "109 mm", "110 mm", "112 mm", "113 mm",
"114 mm", "115 mm", "116 mm", "118 mm", "119 mm", "120 mm",
"129 mm", "133 mm", "71 mm", "73 mm", "78 mm", "79 mm", "80 mm",
"82 mm", "85 mm", "86 mm", "89 mm", "90 mm", "92 mm", "93 mm",
"94 mm", "95 mm", "96 mm", "97 mm", "98 mm", "99 mm"), class = "factor"),
Sex = structure(c(6L, 6L, 3L, 6L, 5L, 3L, 4L, 3L, 3L, 4L), .Label = c("boy",
"f", "F", "fem", "girl", "M"), class = "factor"), Ge = structure(c(1L,
11L, 11L, 2L, 2L, 8L, 4L, 6L, 4L, 4L), .Label = c("32,8",
"33", "33,73", "34", "35,5", "37,29", "37,5", "39,2", "39,9",
"41,3", "43,2"), class = "factor"), Sm = structure(c(1L,
1L, 1L, 2L, 2L, 1L, 1L, 1L, 1L, 1L), .Label = c("N", "S"), class = "factor")), row.names = c(NA,
10L), class = "data.frame")


  [1]: https://i.stack.imgur.com/HNRpv.png
  [2]: https://i.stack.imgur.com/44BpV.png
  • 1

    Do you have the data? What sign of +? Is there an error message? Read that and edit the question.

  • The + sign is what I highlighted in yellow in the photo... after typing the code above, I enter and the "+" sign appears, as if something was missing..

  • Close this code with a parenthesis at the end: ). This "more" means that the function has not been finished (the line asks you to finish it with this +).

  • I can not show the data, but basically it is: When the Hospitarl has the name "H.G.U.GREGORIO MARAÑÓN", I need to leave the variable City = "Madrid", when the hospital has the name "H.U.DE BELLVITGE", the variable City = "Barcelona" and so, successively...

  • Closed the code with the )?

  • yes... did not solve

Show 2 more comments

1 answer

2


The question code has two problems:

Fixed these problems works well.

library(dplyr)
library(stringr)

BWn <- BWn %>% 
  mutate(City = case_when(
    str_detect(HP, "H.U.DE BELLVITGE") ~ "Barcelona", 
    str_detect(HP, "H.CLÍNIC") ~ "Barcelona", 
    str_detect(HP, "H.U.QUIRÓN DEXEUS") ~ "Barcelona", 
    str_detect(HP, "H.G.U.GREGORIO MARAÑÓN") ~ "Madrid", 
    TRUE ~ "Sevilla"))

The first three columns of the result are as follows:.

BWn[1:3]
#   ID                          HP      City
#1   1      H.G.U.GREGORIO MARAÑÓN    Madrid
#2   2            H.U.DE BELLVITGE Barcelona
#3   3            H.U.DE BELLVITGE Barcelona
#4   4                    H.CLÍNIC Barcelona
#5   5           H.U.12 DE OCTUBRE   Sevilla
#6   6      H.G.U.GREGORIO MARAÑÓN    Madrid
#7   7 H.M.I. VIRGEN DE LAS NIEVES   Sevilla
#8   8                 C.U.NAVARRA   Sevilla
#9   9                  H.U.LA PAZ   Sevilla
#10 10                 C.U.NAVARRA   Sevilla

Browser other questions tagged

You are not signed in. Login or sign up in order to post.