How to add 1 column at the end of the another column in the same dataframe in R

Hi,

I have a dataframe. Dataframe contains 4 columns. Now I want to add 2 columns at the end of another 2 columns. So, my final dataframe must contain only 2 columns.

Dataframe looks like

         id                        name    parent_key          ingredients
1       H00607                   Nafcillin    H00091                Cyclosporine
2       H01059                 Norfloxacin   H00091                Cyclosporine
3       H09128               Brexpiprazole    H00091                Cyclosporine
4       H09039                  Eliglustat    H00091                Cyclosporine

I am writing this code

id_name <- data.frame(id = c(all_data[,"id"], all_data[,"parent_key"]),
                      name = c(all_data[,"name"], all_data[,"ingredients"]))

My code is converting 4 columns into 2 columns. But, it is also converting cell values into integer format! The output of my code is

            id          name
1           240           565
2           467           600
3           804            99
4           775           273
5           646           336
6           792           436
7           739           437

Could you tell me, why this is happening and how can I solve this issue?

My expected output is like

         id                        name    
1       H00607                   Nafcillin    
2       H01059                 Norfloxacin  
3       H09128               Brexpiprazole 
4       H09039                  Eliglustat   
5       H00091                Cyclosporine
6       H00091                Cyclosporine
7       H00091                Cyclosporine
8       H00091                Cyclosporine

Your code looks correct to me. You're sure you're not doing anything else that is giving the unexpected results? I would suggest you restart RStudio (Ctrl + Shift + F10) and rerun your code. Below, I recreate your dataset, then I copy and paste your code:

all_data <- data.frame(
  id = c("H00607", "H01059", "H09128", "H09039"),
  name = c("Nafcillin","Norfloxacin","Brexpiprazole","Eliglustat"),
  parent_key = rep("H00091", 4),
  ingredients = rep("Cyclosporine", 4)
)

data.frame(id = c(all_data[,"id"], all_data[,"parent_key"]),
           name = c(all_data[,"name"], all_data[,"ingredients"]))

      id          name
1 H00607     Nafcillin
2 H01059   Norfloxacin
3 H09128 Brexpiprazole
4 H09039    Eliglustat
5 H00091  Cyclosporine
6 H00091  Cyclosporine
7 H00091  Cyclosporine
8 H00091  Cyclosporine

Note: It is always helpful to include your data (or at least a chunk of it) in your question so that your potential helpers will be able to use it. Just pasting the data does not really work. You could use dput(all_data) and post the result so that anyone could easily recreate it.

@gueyenono thanks.

But still showing the same output (integer format)!

suppressPackageStartupMessages({
  library(dplyr)
})
DF <- structure(list(
  id = c("H00607", "H01059", "H09128", "H09039"),
  name = c("Nafcillin", "Norfloxacin", "Brexpiprazole", "Eliglustat"), parent_key = c("H00091", "H00091", "H00091", "H00091"),
  ingredients = c(
    "Cyclosporine", "Cyclosporine", "Cyclosporine",
    "Cyclosporine"
  )
), class = c(
  "spec_tbl_df", "tbl_df", "tbl",
  "data.frame"
), row.names = c(NA, -4L), spec = structure(list(
  cols = list(id = structure(list(), class = c(
    "collector_character",
    "collector"
  )), name = structure(list(), class = c(
    "collector_character",
    "collector"
  )), parent_key = structure(list(), class = c(
    "collector_character",
    "collector"
  )), ingredients = structure(list(), class = c(
    "collector_character",
    "collector"
  ))), default = structure(list(), class = c(
    "collector_guess",
    "collector"
  )), skip = 1L
), class = "col_spec"))

pt1 <- DF[, c(1, 2)]
pt2 <- DF[, c(3, 4)]
colnames(pt2) <- c("id", "name")
combined <- rbind(pt1, pt2)
combined
#> # A tibble: 8 x 2
#>   id     name         
#>   <chr>  <chr>        
#> 1 H00607 Nafcillin    
#> 2 H01059 Norfloxacin  
#> 3 H09128 Brexpiprazole
#> 4 H09039 Eliglustat   
#> 5 H00091 Cyclosporine 
#> 6 H00091 Cyclosporine 
#> 7 H00091 Cyclosporine 
#> 8 H00091 Cyclosporine

Created on 2021-01-02 by the reprex package (v0.3.0.9001)

Could you share your data with dput() if @technocrat's answer does not help you.

1 Like

@gueyenono yes @technocrat's answer is working. But, still I am confused why my code is not working in my devices but the same code is working in your devices!

1 Like

Consider trimming the last three rows in order to keep data in tidy format; ideally, each row is distinct from all other rows, preferably with a unique key field.

@akib62 Send me a private message if you would like to zoom with me. I could take a look at your screen and try to troubleshoot it for you.

1 Like

@gueyenono thanks for your kind support. I will let you know.

This topic was automatically closed 7 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.