gpt4 book ai didi

r - long if else 循环并在 R 中重新编码

转载 作者:行者123 更新时间:2023-12-04 11:59:42 24 4
gpt4 key购买 nike

我知道我的问题很简单,但不适合我。这是小数据集。

mark1 <- c("AB", "BB", "AB", "BB", "BB", "AB", "--", "BB")
mark2 <- c("AB", "AB", "AA", "BB", "BB", "AA", "--", "BB")
mark3 <- c("BB", "AB", "AA", "BB", "BB", "AA", "--", "BB")
mark4 <- c("AA", "AB", "AA", "BB", "BB", "AA", "--", "BB")
mark5 <- c("AB", "AB", "AA", "BB", "BB", "AA", "--", "BB")
mark6 <- c("--", "BB", "AA", "BB", "BB", "AA", "--", "BB")
mark7 <- c("AB", "--", "AA", "BB", "BB", "AA", "--", "BB")
mark8 <- c("BB", "AA", "AA", "BB", "BB", "AA", "--", "BB")
mymark <- data.frame (mark1, mark2, mark3, mark4, mark5, mark6, mark7, mark8)
tmymark <- data.frame (t(mymark))
names (tmymark) <- c("P1", "P2","I1", "I2", "I3", "I4", "KL", "MN")

因此数据集变为:
      P1 P2 I1 I2 I3 I4 KL MN
mark1 AB BB AB BB BB AB -- BB
mark2 AB AB AA BB BB AA -- BB
mark3 BB AB AA BB BB AA -- BB
mark4 AA AB AA BB BB AA -- BB
mark5 AB AB AA BB BB AA -- BB
mark6 -- BB AA BB BB AA -- BB
mark7 AB -- AA BB BB AA -- BB
mark8 BB AA AA BB BB AA -- BB

我想根据 P1 和 P2 比较对 mark1:8 进行分类并提供一个代码,它将创建一个新变量:
loctype <- NULL

if (tmymark$P1 == "AB" & tmymark$P2 == "AB"){
loctype = "<hkxhk>"
} else {
if (tmymark$P1== "AB" & tmymark$P2 == "BB") {
loctype = "<lmxll>"
} else {
if (tmymark$P1 == "AA" & tmymark$P2 == "AB") {
loctype = "<nnxnp>"
} else {
if (tmymark$P1 == "AA" & tmymark$P2 == "BB") {
loctype = "MN"
} else {
if (tmymark$P1 == "BB" & tmymark$P2 == "AA"){
loctype = "MN"
} else {
if (tmymark$P1 == "--" & tmymark$P2 == "AA"){
loctype = "NR"
} else {
if (tmymark$P1 == "AA" & tmymark$P2 == "--"){
loctype = "NR"
} else {
cat ("error wrong input in P1 or P2")
}} }}}}}

在这里,我正在尝试比较 P1 和 P2 值并生成一个新变量。
例如,如果 tmymark$P1 == "AB"& tmymark$P2 == "AB"loctype 应该是 ""。如果不是,第二个条件将是应用程序等。

这是我的错误信息。
Warning messages:
1: In if (tmymark$P1 == "AB" & tmymark$P2 == "AB") { :
the condition has length > 1 and only the first element will be used
2: In if (tmymark$P1 == "AB" & tmymark$P2 == "BB") { :
the condition has length > 1 and only the first element will be used

生成 loctype 向量后,我想使用此变量中的信息重新编码 tmymark:
tmymark1 <- data.frame (loctype, tmymark)      
require(car)
for(i in 2:length(tmymark)){

if (loctype = "<hkxhk>") {
tmymark[[i]] <- recode (x, "AB" = "hk", "BA" = "hk", "AA" = "hh", "BB" = "kk")
} else {
if (loctype = "<lmxll>") {
tmymark[[i]] <- recode ((x, "AB" = "lm", "BA" = "lm", "AA" = "--", "BB" = "kk")
} else {

if (loctype = "<nnxnp>") {
tmymark[[i]] <- recode ((x, "AB" = "np", "BA" = "np", "AA" = "nn", "BB" = "--")
} else {
if (loctype = "MN") {
tmymark[[i]] <- "--"
} esle {
if (loctype = "NR") {
tmymark[[i]] <- "NA"
} else {
cat ("error wrong input code")
} } }}}

我在正确的轨道上吗?

编辑:预期输出
      loctype  P1 P2 I1 I2 I3 I4 KL MN 
mark1 <lmxmm> lm mm lm mm mm lm -- mm
mark2 <hkxhk> hk hk hh kk kk hh -- kk
mark3 <nnxnp> nn np nn -- -- nn -- --
and so on

最佳答案

match绝对是要走的路。我将两个数据框作为键,如下所示:

key <- data.frame(
P1=c("AB", "AB", "AA", "AA", "BB", "--", "AA"),
P2=c("AB", "BB", "AB", "BB", "AA", "AA", "--"),
loctype=c("<hkxhk>", "<lmxll>", "<nnxnp>", "MN", "MN", "NR", "NR"))

key2 <- cbind(
`<hkxhk>` = c("hk","hk","hh","kk"),
`<lmxll>` = c("lm", "lm", "--", "kk"),
`<nnxnp>` = c("np", "np", "nn", "--"),
MN = rep("--", 4),
NR = rep("NA", 4) )
rownames(key2) = c("AB","BA", "AA", "BB")

然后使用 matchkey1获取 loctype (正如贾斯汀也推荐的那样),以及 key2 的行名和列获得所需的替换,使用矩阵索引从键中获取所需的值。
loctype <- key$loctype[match(with(tmymark, paste(P1, P2, sep="\b")), 
with(key, paste(P1, P2, sep="\b")))]
ii <- match(as.vector(as.matrix(tmymark)), rownames(key2))
jj <- rep(match(loctype, colnames(key2)), nrow(tmymark))
out <- as.data.frame(matrix(key2[cbind(ii,jj)], nrow=nrow(tmymark)))
colnames(out) <- colnames(tmymark)
rownames(out) <- rownames(tmymark)
out$loctype <- loctype

结果看起来像这样,其中缺少值是因为我的键中没有这些组合的值。
> print(out, na="")
P1 P2 I1 I2 I3 I4 KL MN loctype
mark1 lm kk lm kk kk lm kk <lmxll>
mark2 hk hk hh kk kk hh kk <hkxhk>
mark3
mark4 nn np nn -- -- nn -- <nnxnp>
mark5 hk hk hh kk kk hh kk <hkxhk>
mark6
mark7
mark8 -- -- -- -- -- -- -- MN

关于r - long if else 循环并在 R 中重新编码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9213940/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com