简体   繁体   中英

conditional ifelse on column when row elements are character

I have a data which looks like this

  Name Status
1    A      A
2    B      C
3    C      B

I want the resulting data to be

> final
  Name Status
1    A      Y
2    B      N
3    C      N

ie where name = Status then status is Y else N

The code I have tried is this. However I get this error

> data$Status <- ifelse(data$Status == data$Name, "Y","N")
Error in Ops.factor(data$Status, data$Name) : 
  level sets of factors are different

I imagine you have levels of the factor that are unique to both Name and Status (ie A, B, and C in name vs. A, B, and D in Status)

To expand on @Conta if these are factors you could include the code

levels(Name)   <- unique(c(levels(Name), levels(Status)))
levels(Status) <- unique(c(levels(Status), levels(Name)))

For example:

> Name   <- factor(c("A","B","C"))
> Status <- factor(c("A","C","D"))
> mydata <- data.frame(Name,Status)
> mydata$Status <- ifelse(mydata$Status == mydata$Name, "Y","N")
    Error in Ops.factor(mydata$Status, mydata$Name) : 
    level sets of factors are different
> 
> levels(Name)   <- unique(c(levels(Name),levels(Status)))
> levels(Status) <- unique(c(levels(Status),levels(Name)))
> 
> Status
[1] A C D
Levels: A C D B
> Name
[1] A B C
Levels: A B C D
> 
> mydata <- data.frame(Name,Status)
> mydata$Status <- ifelse(mydata$Status == mydata$Name, "Y","N")

> mydata
  Name Status
1    A      Y
2    B      N
3    C      N

Character values coming into dataframes are automatically converted to factors (unless stringsAsFactors was set to FALSE). This code should have succeeded with your two-factor dataframe:

final <- cbind( orig[, "Name", drop=FALSE],   # prevents loss of dataframe structure
           Status=ifelse( as.character(orig$Name) == as.character(orig$Status), "Y", "N")
             )

Lightly tested on confusedPerpetually's example:

> final
  Name Status
1    A      Y
2    B      N
3    C      N

I think modifying the levels attribute is particularly dangerous unless it is done as an argument to a call to factor . Using levels<- is a quick way to make big mistakes that are difficult to recover from. I speak from painful experience.

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