1

I have a dataframe that looks like this but with 20 levels.

df <- data.frame(
    x = factor(c("alpha","beta","gamma","delta")),
    y = factor(c("alpha","beta","gamma","delta")),
    z = factor(c("alpha","beta","gamma","delta"))

I would like to create a column within a dataframe based on three other columns.

df$w<-with(df,ifelse(df$z==df$x,df$y,df$x))

But it does not give back the content of the columns, just levels.

MattnDo
  • 444
  • 1
  • 5
  • 17
  • 1
    Possible duplicate of [R adding column which contains bin value of another column](http://stackoverflow.com/questions/5570293/r-adding-column-which-contains-bin-value-of-another-column) – SeldomSeenSlim Feb 27 '17 at 22:46

2 Answers2

3

EDIT: Your code is NOT Wrong. You just have to reconvert your result into factor like this:

 df<-data.frame(B=c("A","B","C","C"), C=c("A","C","B","B"), D=c("B","A","C","A") )   
df$A<-levels(df$B)[with(df,ifelse(df$B==df$C,df$D,df$C))]

To see why this happen you have to see what ifelse does:

debugonce(ifelse)
ifelse(df$B==df$C,df$D,df$C)

Keep in Mind "Factor variables are stored, internally, as numeric variables together with their levels. The actual values of the numeric variable are 1, 2, and so on." In particular ifelse assign to the answer vector boolean values, that is you start with a logical vector. Then based on test comparison, ifelse subset this ans vector assigning "yes" values. So R keep the vector rapresentation.

Briefly something like this happen and you lose the factor rapresentation

   a<-c(TRUE,FALSE)
   a[1]<-df$D[1]
   df$D
   a

Try also this working example (an alternative way to do the same thing)

df<-data.frame(B=c("A","B","C","C"), C=c("A","C","B","B"), D=c("B","A","C","A") )

f<-data.frame(b,c,d)
df
f<-function(x,y,z){
  if(x==y){
    z
  }else{
    y
  }
}

df$A<-unlist(Map(f,df$B,df$C,df$D))
Federico Manigrasso
  • 1,130
  • 1
  • 7
  • 11
-1

Works for me?

 df <- data.frame(B=c("A","B","C","C"), C=c("A","C","B","B"), D=c("B","A","C","A") )

 df$A<-ifelse(df$B==df$C,paste(df$D),paste(df$C))

 return(df)
SeldomSeenSlim
  • 811
  • 9
  • 24
  • 1
    Because your example data isn't a set of factors, like OP has. Try - `df <- data.frame(B=c("A","B","C","C"), C=c("A","C","B","B"), D=c("B","A","C","A") )` – thelatemail Feb 27 '17 at 22:56
  • 1
    No, I mean your input `df` isn't a set of `factor`s, which is necessary to reproduce this problem. – thelatemail Feb 27 '17 at 23:04