简体   繁体   中英

Creating a new column in R

I have a data.frame like the following:

regions       admit    men_age         group    
   1          1234        34              2
   2          3416        51              1
   3          2463        26              3
   4          1762        29              2
   5          2784        31              4
   6          999         42              1
   7          2111        23              2
   8          1665        36              3
   9          2341        21              4
   10         1723        33              1

I would like to create new columns using admit and group as follows:

regions      admit    men_age      group  admit1   admit2   admit3  admit4    
   1          1234        34          2       0     1234       0       0
   2          3416        51          1     3416      0        0       0
   3          2463        26          3       0       0       2463     0
   4          1762        29          2       0      1762      0       0
   5          2784        31          4       0       0        0      2784
   6          999         42          1      999      0        0       0
   7          2111        23          2       0      2111      0       0
   8          1665        36          3       0       0       1665     0
   9          2341        21          4       0       0        0      2341
   10         1723        33          1      1723     0        0       0

In fact, what I want to do is to create four new admit columns according to group column as follows: in admit 1 column, the value for rows where group is 1, put the corresponding admit number, other wise put zero. In admit 2 column, the values for rows where group is 2, put the corresponding admit number, otherwise put zero ans this applies for two other column as well.

I tried a couple of ways to solve it, but failed.

May please someone help me to solve this?

A solution using tidyverse . We can create the columns and then spread them with fill = 0 .

library(tidyverse)

dat2 <- dat %>%
  mutate(group2 = str_c("admit", group), admit2 = admit) %>%
  spread(group2, admit2, fill = 0)
dat2
#    regions admit men_age group admit1 admit2 admit3 admit4
# 1        1  1234      34     2      0   1234      0      0
# 2        2  3416      51     1   3416      0      0      0
# 3        3  2463      26     3      0      0   2463      0
# 4        4  1762      29     2      0   1762      0      0
# 5        5  2784      31     4      0      0      0   2784
# 6        6   999      42     1    999      0      0      0
# 7        7  2111      23     2      0   2111      0      0
# 8        8  1665      36     3      0      0   1665      0
# 9        9  2341      21     4      0      0      0   2341
# 10      10  1723      33     1   1723      0      0      0

DATA

dat <- read.table(text = "regions       admit    men_age         group    
   1          1234        34              2
                  2          3416        51              1
                  3          2463        26              3
                  4          1762        29              2
                  5          2784        31              4
                  6          999         42              1
                  7          2111        23              2
                  8          1665        36              3
                  9          2341        21              4
                  10         1723        33              1",
                  header = TRUE)

A Base R solution would be using ifelse() . Supposed you data.frame is x , you could do this:

# create the columns with the selected values
for( i in 1:4 ) x[ i + 4 ] <- ifelse( x$group == i, x$admit, 0 )
# rename the columns to your liking
colnames( x )[ 5:8 ] <- c( "admit1", "admit2", "admit3", "admit4" )

This gives you

> x
   regions admit men_age group admit1 admit2 admit3 admit4
1        1  1234      34     2      0   1234      0      0
2        2  3416      51     1   3416      0      0      0
3        3  2463      26     3      0      0   2463      0
4        4  1762      29     2      0   1762      0      0
5        5  2784      31     4      0      0      0   2784
6        6   999      42     1    999      0      0      0
7        7  2111      23     2      0   2111      0      0
8        8  1665      36     3      0      0   1665      0
9        9  2341      21     4      0      0      0   2341
10      10  1723      33     1   1723      0      0      0

If you don't like the explicit naming, you could do it in the for() loop already:

for( i in 1:4 ) 
{ 
    adm <- paste ( "admit", i, sep = "" )
    x[ adm ] <- ifelse( x$group == i, x$admit, 0 )
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM