我需要用条件复制一列或变量

问题描述 投票:0回答:2

我正在处理一些数据,几乎 4 个变量具有以下特征:

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

我需要创建或复制这个变量,从数字中删除“-”,但保持数字与名称之间的关系,即

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

我试图在网上找到一些信息,但是我找不到有用的东西来解决这个问题。

r duplicates code-duplication cbind
2个回答
0
投票

这不像我希望写的那样优雅。我用 sapply() 和 mutate(across()) 尝试了不同的方法,但我无法让它们工作。

library(tidyverse)

# Clean up extra "-" in starting position.
df1 <- data.frame(apply(df, 2, function(x) gsub("^-","",x)))

# Use separate() multiple times.
df1 %>% separate(., DptResidence, "-", into=c("CodeResidence","DptResidence")) %>% separate(., DeathIndex, "-", into=c("DeathIndexCode","DeathIndex")) 

  CodeResidence DptResidence DeathIndexCode DeathIndex
1             1  Not defined              0         No
2            54        North              1        Yes
3            81        South              0         No

0
投票

使用 base R 你可以做:

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
© www.soinside.com 2019 - 2024. All rights reserved.