-1

How do I add a column to a dataframe in R based on values in another column of a dataframe ?

For eg if I have one column as x$n = [1,2,3,4,5,6] (values in other colums dont exactly matter. And I want another column as a 'category' column that assigns value 0 if x$n < 2, 1 if x$n is between 3 and 4 and 3 if x$n > 4. So that my corresponding column would be x$category = [0,0,1,1,2,2]

A5C1D2H2I1M1N2O1R2T1
  • 190,393
  • 28
  • 405
  • 485
Anurag Mishra
  • 1,007
  • 6
  • 16
  • 23

2 Answers2

1

Using cut:

within(x, category <- as.integer(cut(n,c(-Inf,2,4,Inf)))-1)

Using ifelse:

within(x, category <- ifelse(n>4, 2, ifelse(n>2, 1, 0)))

Using implicit boolean -> integer coercion::

within(x, category <- (n>2) + (n>4))
Ferdinand.kraft
  • 12,579
  • 10
  • 47
  • 69
0

If you have:

x = data.frame(n = 1:6)

and only have three categories, then the easiest solution would be:

x$category = 0
x$category[x$n > 2] = 1
x$category[x$n > 4] = 2

If you want to be really clever, then you could do:

x$category = floor(x$n/2.5)

The floor function` just rounds down.

csgillespie
  • 59,189
  • 14
  • 150
  • 185