在另一列中创建基于4值的新列。

时间:2021-12-06 00:36:47

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

我想在另一列中创建一个基于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.

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

在R中怎么做呢?我需要有人帮忙解决这个问题。我已经试过了,但没有办法。谢谢

4 个解决方案

#1


15  

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.

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

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

#2


22  

You could use nested ifelse:

您可以使用嵌套的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...

在这个简单的例子中,它是过量的,但是对于更复杂的…

#3


5  

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.

这里有一个使用汽车的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]

#4


1  

You could have a look at ?symnum.

你可以看看,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.

应该让你接近。

#1


15  

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.

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

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

#2


22  

You could use nested ifelse:

您可以使用嵌套的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...

在这个简单的例子中,它是过量的,但是对于更复杂的…

#3


5  

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.

这里有一个使用汽车的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]

#4


1  

You could have a look at ?symnum.

你可以看看,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.

应该让你接近。