根据另一列中的 4 个值创建新列

发布于 2024-12-07 21:03:35 字数 219 浏览 4 评论 0原文

我想根据另一列中的 4 个值创建一个新列。

if col1=1 then col2= G;
if col1=2 then col2=H;
if col1=3 then col2=J;
if col1=4 then col2=K.

我如何在 R 中执行此操作? 请我需要有人帮助解决这个问题。我尝试过 if/else 和 ifelse 但似乎都不起作用。谢谢

I want to create a new column based on 4 values in another column.

if col1=1 then col2= G;
if col1=2 then col2=H;
if col1=3 then col2=J;
if col1=4 then col2=K.

HOW DO I DO THIS IN R?
Please I need someone to help address this. I have tried if/else and ifelse but none seems to be working. Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

孤独岁月 2024-12-14 21:03:35

您可以使用嵌套的ifelse

col2 <- ifelse(col1==1, "G",
        ifelse(col1==2, "H",
        ifelse(col1==3, "J",
        ifelse(col1==4, "K",
                        NA  )))) # all other values map to NA

在这个简单的情况下,它是多余的,但对于更复杂的情况......

You could use nested ifelse:

col2 <- ifelse(col1==1, "G",
        ifelse(col1==2, "H",
        ifelse(col1==3, "J",
        ifelse(col1==4, "K",
                        NA  )))) # all other values map to NA

In this simple case it's overkill, but for more complicated ones...

空城之時有危險 2024-12-14 21:03:35

您有一个特殊情况,即查找索引为整数 1:4 的值。这意味着您可以使用向量索引轻松地解决您的问题。

首先,创建一些示例数据:

set.seed(1)
dat <- data.frame(col1 = sample(1:4, 10, replace = TRUE))

接下来,定义查找值,并使用 [ 子集化来查找所需的结果:

values <- c("G", "H", "J", "K")
dat$col2 <- values[dat$col1]

结果:

dat
   col1 col2
1     2    H
2     2    H
3     3    J
4     4    K
5     1    G
6     4    K
7     4    K
8     3    J
9     3    J
10    1    G

更一般地,您可以使用 [ 子集化组合用 match 来解决此类问题:

index <- c(1, 2, 3, 4)
values <- c("G", "H", "J", "K")
dat$col2 <- values[match(dat$col1, index)]
dat
   col1 col2
1     2    H
2     2    H
3     3    J
4     4    K
5     1    G
6     4    K
7     4    K
8     3    J
9     3    J
10    1    G

You have a special case of looking up values where the index are integer numbers 1:4. This means you can use vector indexing to solve your problem in one easy step.

First, create some sample data:

set.seed(1)
dat <- data.frame(col1 = sample(1:4, 10, replace = TRUE))

Next, define the lookup values, and use [ subsetting to find the desired results:

values <- c("G", "H", "J", "K")
dat$col2 <- values[dat$col1]

The results:

dat
   col1 col2
1     2    H
2     2    H
3     3    J
4     4    K
5     1    G
6     4    K
7     4    K
8     3    J
9     3    J
10    1    G

More generally, you can use [ subsetting combined with match to solve this kind of problem:

index <- c(1, 2, 3, 4)
values <- c("G", "H", "J", "K")
dat$col2 <- values[match(dat$col1, index)]
dat
   col1 col2
1     2    H
2     2    H
3     3    J
4     4    K
5     1    G
6     4    K
7     4    K
8     3    J
9     3    J
10    1    G
你又不是我 2024-12-14 21:03:35

有多种方法可以做到这一点,但这里是一种。

set.seed(357)
mydf <- data.frame(col1 = sample(1:4, 10, replace = TRUE))
mydf$col2 <- rep(NA, nrow(mydf))
mydf[mydf$col1 == 1, ][, "col2"] <- "A"
mydf[mydf$col1 == 2, ][, "col2"] <- "B"
mydf[mydf$col1 == 3, ][, "col2"] <- "C"
mydf[mydf$col1 == 4, ][, "col2"] <- "D"

   col1 col2
1     1    A
2     1    A
3     2    B
4     1    A
5     3    C
6     2    B
7     4    D
8     3    C
9     4    D
10    4    D

这是使用 carrecode 的一个。

library(car)
mydf$col3 <- recode(mydf$col1, "1" = 'A', "2" = 'B', "3" = 'C', "4" = 'D')

此问题的另一个问题:

mydf$col4 <- c("A", "B", "C", "D")[mydf$col1]

There are a number of ways of doing this, but here's one.

set.seed(357)
mydf <- data.frame(col1 = sample(1:4, 10, replace = TRUE))
mydf$col2 <- rep(NA, nrow(mydf))
mydf[mydf$col1 == 1, ][, "col2"] <- "A"
mydf[mydf$col1 == 2, ][, "col2"] <- "B"
mydf[mydf$col1 == 3, ][, "col2"] <- "C"
mydf[mydf$col1 == 4, ][, "col2"] <- "D"

   col1 col2
1     1    A
2     1    A
3     2    B
4     1    A
5     3    C
6     2    B
7     4    D
8     3    C
9     4    D
10    4    D

Here's one using car's recode.

library(car)
mydf$col3 <- recode(mydf$col1, "1" = 'A', "2" = 'B', "3" = 'C', "4" = 'D')

One more from this question:

mydf$col4 <- c("A", "B", "C", "D")[mydf$col1]
萌吟 2024-12-14 21:03:35

您可以查看 ?symnum

就你而言,类似:

col2<-symnum(col1, seq(0.5, 4.5, by=1), symbols=c("G", "H", "J", "K"))

应该让你接近。

You could have a look at ?symnum.

In your case, something like:

col2<-symnum(col1, seq(0.5, 4.5, by=1), symbols=c("G", "H", "J", "K"))

should get you close.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文