Reputation: 1944
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
i.e. 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
Upvotes: 0
Views: 60
Reputation: 263352
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.
Upvotes: 1
Reputation: 23
I imagine you have levels of the factor that are unique to both Name and Status (i.e. 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
Upvotes: 1