Tidy up a multi-column table based on a single column


#1

Hello I'm trying to tidy up a multi-column table based on one column but my code is not working

this the table
Capture

my code

tgr = tgr[order(tgr$textForPos),]

the expected result is
1 -> charactar
1->charactar


any one to help me please?


#2

There's nothing obviously wrong with the code you posted ā€” I can't easily access your data (here are some methods for sharing data that are better than a screenshot), but if I invent some similar sample data the code works as I would expect it to:

set.seed(42)

tgr <- data.frame(
  textForPos = rep(sample.int(10, size = 5), 2),
  tablePos = rep(sample(row.names(mtcars), 5), 2),
  stringsAsFactors = FALSE
)

tgr
#>    textForPos          tablePos
#> 1          10 Chrysler Imperial
#> 2           9       AMC Javelin
#> 3           3 Hornet Sportabout
#> 4           6    Toyota Corolla
#> 5           4    Ford Pantera L
#> 6          10 Chrysler Imperial
#> 7           9       AMC Javelin
#> 8           3 Hornet Sportabout
#> 9           6    Toyota Corolla
#> 10          4    Ford Pantera L

tgr[order(tgr$textForPos),]
#>    textForPos          tablePos
#> 3           3 Hornet Sportabout
#> 8           3 Hornet Sportabout
#> 5           4    Ford Pantera L
#> 10          4    Ford Pantera L
#> 4           6    Toyota Corolla
#> 9           6    Toyota Corolla
#> 2           9       AMC Javelin
#> 7           9       AMC Javelin
#> 1          10 Chrysler Imperial
#> 6          10 Chrysler Imperial

Created on 2018-07-25 by the reprex package (v0.2.0).

Can you explain in more detail what's not working for you?

You might also want to take a look at these guidelines for asking questions about R code:


#3

The problem was due to the fact that I was try to put the result in the same variable

tgr = tgr[order(tgr$textForPos),]

I have just create another one and it work well

ttrtrtgr = tgr[order(tgr2$textForPos),]

Thank for the help


#4

As an alternative, a tidyverse solution would be to use arrange(textForPos).


#5

As an FYI, in R, the primary way to assign a new variable is using <-, as in new_var <- old_var. There is a bit of an explanation here.


#6

I'm glad you've got things working for you, but what you report is a bit odd. The code you posted is the standard way to reorder the rows in a data frame and keep the changes (by assigning the reordered data frame to the same name as the original data frame), so I can't imagine why tgr = tgr[order(tgr$textForPos), ] wouldn't work. Indeed, it does work if I try it:

set.seed(42)

tgr <- data.frame(
  textForPos = rep(sample.int(10, size = 5), 2),
  tablePos = rep(sample(row.names(mtcars), 5), 2),
  stringsAsFactors = FALSE
)

tgr
#>    textForPos          tablePos
#> 1          10 Chrysler Imperial
#> 2           9       AMC Javelin
#> 3           3 Hornet Sportabout
#> 4           6    Toyota Corolla
#> 5           4    Ford Pantera L
#> 6          10 Chrysler Imperial
#> 7           9       AMC Javelin
#> 8           3 Hornet Sportabout
#> 9           6    Toyota Corolla
#> 10          4    Ford Pantera L

tgr <- tgr[order(tgr$textForPos),]

tgr
#>    textForPos          tablePos
#> 3           3 Hornet Sportabout
#> 8           3 Hornet Sportabout
#> 5           4    Ford Pantera L
#> 10          4    Ford Pantera L
#> 4           6    Toyota Corolla
#> 9           6    Toyota Corolla
#> 2           9       AMC Javelin
#> 7           9       AMC Javelin
#> 1          10 Chrysler Imperial
#> 6          10 Chrysler Imperial

Created on 2018-07-26 by the reprex package (v0.2.0).

(I'm personally Team <-, but choice of assignment operator shouldn't matter in this case).

I'm noticing that you're now using the textForPos variable from a different data frame ā€” tgr2 ā€” to order by, and assigning the re-ordered data frame to a third name. This shouldn't be necessary, unless you really do want to keep both the re-ordered and originally-ordered data frames, and either way I'm not sure what role tgr2 is playing here?