简体   繁体   中英

R - How to choose values for new column based on condition that values are equal in another column

I have a dataframe, "data" below, and I am trying to add a new column to the end of it based on a condition. If the column data$code matches with a value in the first column of my dataframe "linked", I want the new column to take the corresponding value in the second column of "linked". If the column data$code matches with a value in the second column of my dataframe "linked", I want the new column to take the corresponding value in the first column of "linked". If the column data$code does not match any values in either column, I want to return NA. I tried the code below:

data$new<- ifelse(data$code %in% linked[,1],linked[linked[,1] == data$code,2],ifelse(data$code == linked[,2],linked[linked[,2] %in% data$code,1],NA))

There is no error message returned, however, I am not getting the correct corresponding values in the new column, they are mixed up for some reason. What am I doing wrong?

head(linked)
    Col1   Col2     
1 123456 654321 
2 234567 123456 
3 999999 543210 
4 102938 546378 
5 887765 000998 
6 564738 222345

    head(data)
      code       x     y        z
1   123456       1     2        0
2   999999       2     3        0
3   000998       3     4        0
4   106813       4     6        0
5   222345       5     6        0
6   106815       6     5        0

what i would like as a result is:

head(data)
      code       x     y        z        new
1   123456       1     2        0     654321 
2   999999       2     3        0     543210
3   000998       3     4        0     887765
4   106813       4     6        0         NA
5   222345       5     6        0     564738
6   106815       6     5        0         NA

You could try this:

data$col.new <- linked$Col2[match(data$code,linked$Col1)]
data$col.new[is.na(data$col.new)] <- linked$Col1[match(data$code[is.na(data$col.new)],linked$Col2)]

data
#     code x y z col.new
# 1 123456 1 2 0  654321
# 2 999999 2 3 0  543210
# 3 000998 3 4 0  887765
# 4 106813 4 6 0    <NA>
# 5 222345 5 6 0  564738
# 6 106815 6 5 0    <NA>

IMHO this will do what you want:

merge(data, linked, by.x="code", by.y="Col1", all.x=TRUE)

with your heads of the dataframes I get:

linked <- read.table(header=TRUE, colClasses="character", text=
'Col1   Col2     
1 123456 654321 
2 234567 123456 
3 999999 543210 
4 102938 546378 
5 887765 000998 
6 564738 222345')

data <- read.table(header=TRUE, colClasses="character", text=
'code       x     y        z
1   123456       1     2        0
2   999999       2     3        0
3   000998       3     4        0
4   106813       4     6        0
5   222345       5     6        0
6   106815       6     5        0')

d1 <- merge(data, linked, by.x="code", by.y="Col1", all.x=TRUE)
d2 <- merge(d1, linked, by.x="code", by.y="Col2", all.x=TRUE)
d2$col.new <- with(d2, ifelse(!is.na(Col2), Col2, Col1))
d2

.

> d2
    code x y z   Col2   Col1 col.new
1 000998 3 4 0   <NA> 887765  887765
2 106813 4 6 0   <NA>   <NA>    <NA>
3 106815 6 5 0   <NA>   <NA>    <NA>
4 123456 1 2 0 654321 234567  654321
5 222345 5 6 0   <NA> 564738  564738
6 999999 2 3 0 543210   <NA>  543210

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