0

I have different columns A, B, C in a data frame. A has values 7,7,5 B has a,f,g, and C has 3,2,1 values. I need to sort only the values of C alphabetically leaving the whole data frame alone. I see that the order() function that let's order the entire data frame with respect to a column but I need to sort a single column.

Kay
  • 602
  • 8
  • 24
edlynch82
  • 1
  • 2

2 Answers2

0

You can do it via sort or via order:

d <- data.frame(a = c(7, 7, 5), b = c("a", "f", "g"), c = c(3, 2, 1))

d$c_sorted1 <- sort(d$c)

d$c_sorted2 <- d$c[order(d$c)]
Bernhard
  • 3,801
  • 1
  • 11
  • 21
-1

As Answered by AllanCameron in the comments. Thanks to him we have got an answer.

If your data frame is called df you can do df$C <- sort(df$C). This is more the kind of thing you can look up in a textbook or online than having to ask on SO. Also, order can order a single column if you like: df$C <- df$C[order(df$C)]

edlynch82
  • 1
  • 2