0
df <- c("A", "B", "C", "D")
df1 <- c("Dog", "Apple", "Bat", "Cat")
mydata <- data.frame(df, df1)

mydata
  df   df1
1  A   Dog
2  B Apple
3  C   Bat
4  D   Cat

I have a long data set following this pattern. How can I arrange A = Apple, B = Bat............ creating a new column?

Further, is it possible for unequal row and column data?

Mark
  • 7,785
  • 2
  • 14
  • 34
  • Does this answer your question? [How does one reorder columns in a data frame?](https://stackoverflow.com/questions/5620885/how-does-one-reorder-columns-in-a-data-frame) – Mark Jul 05 '23 at 09:24
  • Do you mean that you want to make a new column which is just the second column but sorted in alphabetical order? – rps1227 Jul 05 '23 at 09:28
  • I am trying to align with the rows which are not alphabetical. What if I have a long range of data c(A, B, C, D, .....) and another data set with c (Nepal, Bhutan, India, Bangladesh.... ), and I want D = Nepal, C = Bhutan, B = India and A = Bangladesh? – Nanda Kumar Maharjan Jul 09 '23 at 07:53

1 Answers1

0

you may try

mydata$new <- sort(mydata$df1)

  df   df1   new
1  A   Dog Apple
2  B Apple   Bat
3  C   Bat   Cat
4  D   Cat   Dog

jkatam
  • 2,691
  • 1
  • 4
  • 12