0

I have the following data frame:

col1 <- c("a", "b", "c")
col2 <- c("c", "a", "d")
col3 <- c("b", "c", "a")

df <- data.frame(col1,col2,col3)

I want to create a new column in this data frame that has, for each row, the ordered list of the columns col1, col2, col3. So, for the first row it would be a list like "a", "b", "c".

The way I'm handling it is to create a loop but since I have 50k rows, it's quite inefficient, so I'm looking for a better solution.

rown <- nrow(df)
i = 0
while(i<rown){
  i = i +1
  col1 <- df$col1[i]
  col2 <- df$col2[i]
  col3 <- df$col3[i]

  col1 <- as.character(col1)
  col2 <- as.character(col2)
  col3 <- as.character(col3)

  list1 <- c(col1, col2, col3)

  list1 <- list1[order(sapply(list1, '[[', 1))]

  a <- list1[1]
  b <- list1[2]
  c <- list1[3]


  df$col.list[i] <- paste(a, b, c, sep = " ")


}

Any ideas on how to make this code more efficient?

EDIT: the other question is not relevant in my case since I need to paste the three columns after sorting each row, so it's the paste statement that is dynamic, I'm not trying to change the data frame by sorting.

Expected output:

col1 col2 col3 col.list
a     c     b   a b c
b     a     c   a b c
c     d     a   a c d
Barbara
  • 1,118
  • 2
  • 11
  • 34
  • Or to create the new column with the strings, `apply(df,1, function(i) toString(sort(i)))` – Sotos Oct 30 '17 at 07:58
  • @RonakShah I'm not trying to sort the whole data frame. I'm trying to create a new column in which I paste three columns and the order in which they are pasted is sorted depending on the value each row has. Don't understand how this is a duplicate of the question you linked... – Barbara Oct 30 '17 at 08:10
  • So you want to create three new columns and at each row, it's sorted value of that existing 3 rows? Also mind creating creating sample expected output? – amrrs Oct 30 '17 at 08:15
  • @RonakShah , amrrs I added the expected result. I tried the code but it doesn't seem to work, I would prefer not using Sotos' solution because I cannot change the sorting of the values in the columns, so it would mean creating three new columns, then sorting, then pasting and then deleting the new columns – Barbara Oct 30 '17 at 08:23
  • 3
    This would work : `df$col.list <- apply(df, 1, function(x) paste(sort(x), collapse = " "))` – Ronak Shah Oct 30 '17 at 08:24
  • @RonakShah should the sort(x) be changed with sort(col1,col2, col3)? I'm trying like that and directly with the list but I get a `dim(X) must have a positive length`error – Barbara Oct 30 '17 at 08:37
  • @RonakShah restarted the session and it worked perfectly! thanks a lot! – Barbara Oct 30 '17 at 09:04

0 Answers0