How to swap serial number column with first column of data?

Can someone please let me know how i can swap first column (serial number) with first column of data? Like this:
Screenshot 2020-10-12 140125

Is this what you mean?

(sample_df <- data.frame(col1 = letters[1:5],
                        col2 = rnorm(5),
                        col3 = rnorm(5)))
#>   col1       col2        col3
#> 1    a -1.1516010  0.04966653
#> 2    b  0.4765599  2.10323658
#> 3    c  0.8362574 -1.45946950
#> 4    d  0.1447962 -0.37182843
#> 5    e  0.2714111  0.10338340

rownames(sample_df) <- sample_df$col1
sample_df[,-1]
#>         col2        col3
#> a -1.1516010  0.04966653
#> b  0.4765599  2.10323658
#> c  0.8362574 -1.45946950
#> d  0.1447962 -0.37182843
#> e  0.2714111  0.10338340

Created on 2020-10-12 by the reprex package (v0.3.0)

If you need more specific help, please provide a proper REPRoducible EXample (reprex) illustrating your issue.

It is not working somehow,

dat <- read.csv("F:/Files_Figures/mfa_file.csv", stringsAsFactors = F)
colnames(dat)[1] = "sample_name"
dat1 <- subset(dat, select = c(1,23:31,34:41,43))
rownames(dat1) <- dat1$col1

Here is data:

squaads <- tibble::tribble(
                          ~ss, ~Yellow_A, ~Purple_A, ~Dullness_A, ~Smoothness_A, ~Gritty_A, ~SweetAromatics_AR, ~MustyEarthy_AR,
             "Rus",         6,         0,           9,             4,         2,                1.5,             3.5,
                 "Von",         0,       1.5,           9,             8,         0,                  0,               4,
                 "Made",         6,         0,          12,            11,         0,                  0,               4,
               "Rido",       1.5,         0,        11.5,           1.5,       1.5,                  0,               0,
                "P-3",        12,         0,         9.5,             9,         2,                  0,               0,
                     "Vry",        13,         0,           9,            12,         0,                  2,               0,
                 "COR",         2,         0,          12,          12.5,         0,                  0,               0,
                "CO0RU",       1.5,         0,          11,            12,         0,                  0,               0,
              "Cant",         2,         0,          12,            12,         0,                  0,             2.5,
               "AC99",        11,         0,          11,             9,         2,                1.5,               0,
                   "Atlic",       2.5,         0,          11,            12,         0,                  0,               0,
             "Purpy",         0,         7,          11,            11,       1.5,                  0,             3.5
             )
head(squads)
#> Error in head(squads): object 'squads' not found

Created on 2020-10-12 by the reprex package (v0.3.0)

It works with your sample data, please try to provide a proper reprex showing your issue.

squads <- data.frame(
   stringsAsFactors = FALSE,
                 ss = c("Rus","Von","Made","Rido",
                        "P-3","Vry","COR","CO0RU","Cant","AC99","Atlic",
                        "Purpy"),
           Yellow_A = c(6, 0, 6, 1.5, 12, 13, 2, 1.5, 2, 11, 2.5, 0),
           Purple_A = c(0, 1.5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7),
         Dullness_A = c(9, 9, 12, 11.5, 9.5, 9, 12, 11, 12, 11, 11, 11),
       Smoothness_A = c(4, 8, 11, 1.5, 9, 12, 12.5, 12, 12, 9, 12, 11),
           Gritty_A = c(2, 0, 0, 1.5, 2, 0, 0, 0, 0, 2, 0, 1.5),
  SweetAromatics_AR = c(1.5, 0, 0, 0, 0, 2, 0, 0, 0, 1.5, 0, 0),
     MustyEarthy_AR = c(3.5, 4, 4, 0, 0, 0, 0, 0, 2.5, 0, 0, 3.5)
)

rownames(squads) <- squads$ss
squads[,-1]
#>       Yellow_A Purple_A Dullness_A Smoothness_A Gritty_A SweetAromatics_AR
#> Rus        6.0      0.0        9.0          4.0      2.0               1.5
#> Von        0.0      1.5        9.0          8.0      0.0               0.0
#> Made       6.0      0.0       12.0         11.0      0.0               0.0
#> Rido       1.5      0.0       11.5          1.5      1.5               0.0
#> P-3       12.0      0.0        9.5          9.0      2.0               0.0
#> Vry       13.0      0.0        9.0         12.0      0.0               2.0
#> COR        2.0      0.0       12.0         12.5      0.0               0.0
#> CO0RU      1.5      0.0       11.0         12.0      0.0               0.0
#> Cant       2.0      0.0       12.0         12.0      0.0               0.0
#> AC99      11.0      0.0       11.0          9.0      2.0               1.5
#> Atlic      2.5      0.0       11.0         12.0      0.0               0.0
#> Purpy      0.0      7.0       11.0         11.0      1.5               0.0
#>       MustyEarthy_AR
#> Rus              3.5
#> Von              4.0
#> Made             4.0
#> Rido             0.0
#> P-3              0.0
#> Vry              0.0
#> COR              0.0
#> CO0RU            0.0
#> Cant             2.5
#> AC99             0.0
#> Atlic            0.0
#> Purpy            3.5

Created on 2020-10-12 by the reprex package (v0.3.0)

1 Like

@andresrcs It worked, i was missing column names.

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.