2
I am trying to create a column x, with the value of column A, if the value of column B == X, if not = 0
Example:
sales<-c(100,140,200,300,20,1000,200,3000)
vendedor<-c("A","B","A","B","C","C","D","A")
regiao<-c("North","South","East","North","South","North","East","South"")
df<-data.frame(seller, region, sales)
newcol would be :
if region == 'north' take the seller’s value, Else 0
outworking
newcol = "A",0,0,"B",0,"C",0,0
For some reason I have the return of numbers, not the associated value A B C or D df$newcol [1] 1 0 0 2 0 3 0 0
– Marcos Godoy
Column with ABCD is a factor? If yes use ifelse so
ifelse(df$regiao == "Norte", as.character(df$vendedor), 0)
works. Oras.character(vendedor)
in the example with tidyverse– Jorge Mendes
I just came in here again to comment on this, the column was as factor, I transformed it with the.Haracter and it worked :) Thanks for the help!
– Marcos Godoy