简体   繁体   中英

Replacing values in one column with another based on a 3rd column matching a 4th

I'm working with the following example:

Original     Modified     New_Orig     New
1            2            1            0
2            4            1            0
3            6            4            0
4            8            5            0
5            10           5            0
6            12           5            0
7            14           5            0
8            16           5            0
9            18           9            0
10           20           10           0

I want to replace values in New with values from Modified if New_Orig matches with any value in Original .

Ideally New will look like this:

New
2  
2
8
10
10
10
10
10
18
20  

Any help much appreciated.

Kind regards,

Here, a new column New is created:

within(dat, New <- Modified*(New_Orig == Original))

   Original Modified New_Orig New
1         1        2        1   2
2         2        4        1   0
3         3        6        4   0
4         4        8        5   0
5         5       10        5  10
6         6       12        5   0
7         7       14        5   0
8         8       16        5   0
9         9       18        9  18
10       10       20       10  20

Update

Match values and choose appropriate value from Modified :

within(dat, New <- Modified[match(New_Orig, Original)])

   Original Modified New_Orig New
1         1        2        1   2
2         2        4        1   2
3         3        6        4   8
4         4        8        5  10
5         5       10        5  10
6         6       12        5  10
7         7       14        5  10
8         8       16        5  10
9         9       18        9  18
10       10       20       10  20

Since @rcs gave exactly the answer I would give, I thought I would show you an alternative approach to creating this "New" column rather than initializing it as all zeroes.

data <- data.frame(Original = 1:10,
                   Modified = seq(2, 20, 2),
                   New_Orig = c(1, 1, 4, 5, 5, 
                                5, 5, 5, 9, 10))
within(data, {
    New <- ifelse(Original == New_Orig, Modified, 0)
})
#    Original Modified New_Orig New
# 1         1        2        1   2
# 2         2        4        1   0
# 3         3        6        4   0
# 4         4        8        5   0
# 5         5       10        5  10
# 6         6       12        5   0
# 7         7       14        5   0
# 8         8       16        5   0
# 9         9       18        9  18
# 10       10       20       10  20

Try the following:

v <- dat$New_Orig==dat$Original # this gives a logical vector,
                                # you could also use which(dat$New_Orig==dat$Original)
                                # to obtain the indices
dat[v, "New"] <- dat[v, "Modified"]

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