0

I'm working on some data and almost 4 variables has this characteristics:

DptResidence          DeathIndex
-1-Not defined        0-No
54-North              1-Yes
81-South              0-No

I need to create or duplicate this variables erasing "-" from numbers, but maintaining relationship between numbers with names, i.e.

CodeResidence        DptResidence      DeathIndexCod     DeathIndex
1                    Not defined       0                 No
54                   North             1                 Yes
81                   South             0                 No

I've tried to find some information online, however I can't find something useful to solve this problem.

Phil
  • 7,287
  • 3
  • 36
  • 66
Sophia
  • 1

1 Answers1

0

Using base R you could do:

d <- data.frame(gsub("(\\d)-","\\1,",as.matrix(df)))
nms <- c("CodeResidence", "DptResidence", "DeathIndexCod", "DeathIndex")
read.csv(text = do.call(paste, c(sep=',', d)), col.names = nms, header = FALSE)

  CodeResidence DptResidence DeathIndexCod DeathIndex
1            -1  Not defined             0         No
2            54        North             1        Yes
3            81        South             0         No
Onyambu
  • 67,392
  • 3
  • 24
  • 53
  • Hi, thank you so much for your kidness help. However, using this code, rstudio shows this error= datos <- data.frame(gsub("(\\d)-","\\1,",as.matrix(df))) Error in as.vector(x, mode) : cannot coerce type 'closure' to vector of type 'any , And I don't know how to solve it. I really appreciate your answer. – Sophia Apr 09 '23 at 22:01
  • @Sophia `df` should by your `data.frame`. replace `df` with the name of your data – Onyambu Apr 09 '23 at 22:10