How do I insert values that correspond to each other?

Hello community.

I want to insert list$Letters into gas_total as gas_total$tukey.

But even if I insert it, gas_total$tukey is just in alphabetical order and does not match gas_total$sample.

How can I insert list$Letters as gas_total$tukey so that list$rownames matches gas_total$sample?

Thank you.

> gas_total
# A tibble: 6 × 3
  sample  total_mean total_sd
  <chr>        <dbl>    <dbl>
1 10x           3.26    0.313
2 1x            1.3     0.122
3 2x            1.52    0.303
4 5x            2.44    0.792
5 EtOH          4.1     0.235
6 control       5       0.383

> list
        Letters monospacedLetters
control       a             a
EtOH          b              b
10x           c               c
5x            d                d
2x            e                 e
1x            e                 e

> gas_total$tukey=list$Letters
> gas_total
# A tibble: 6 × 4
  sample  total_mean total_sd tukey
  <chr>        <dbl>    <dbl> <chr>
1 10x           3.26    0.313 a    
2 1x            1.3     0.122 b    
3 2x            1.52    0.303 c    
4 5x            2.44    0.792 d    
5 EtOH          4.1     0.235 e    
6 control       5       0.383 e  

You need to do a join or merge of the two data frames. Here is one possible solution.

library(dplyr)
tmp <- list[, c("control","Letters")]
gas_total <- inner_join(gas_total, tmp, by = c("sample" = "control"))
colnames(gas_total)[4] <- "tukey"
1 Like

Thank you for your prompt reply and suggestion.

But can't do that...

> library(dplyr)
> tmp <- list[, c("control","Letters")]

Error in list[, c("control", "Letters")] : 
  object of type 'builtin' is not subsettable

You get that error because there is no object named list, so R thinks list refers to the function list(). Your first post shows that you have a data frame named list. What is the name of the object that has the columns control and Letters? Use the name of that in the code where I wrote list.

Thank you so much !

I did it !

> gas_total
# A tibble: 6 × 4
  sample  total_mean total_sd tukey
  <chr>        <dbl>    <dbl> <chr>
1 10x           3.26    0.313 c    
2 1x            1.3     0.122 e    
3 2x            1.52    0.303 e    
4 5x            2.44    0.792 d    
5 EtOH          4.1     0.235 b    
6 control       5       0.383 a 

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.