0

I found how to print selected column by their names on R but not how to print the column names.

My data set is called t and I tried that :

print(colnames(t[3:ncol(t)]))

But it doesn't work. It prints all the columns. I could print the selected columns by removing colnames but it selected the column and not the column name.

  • 1
    Your code works for me: `t = iris colnames(t) print(colnames(t[3:ncol(t)]))` – tauft Apr 29 '22 at 10:51
  • what gives `str(t)` – TarJae Apr 29 '22 at 10:56
  • 1
    As tauft mentioned, your code is working with base R example datasets (`iris`, `mtcars`, *etc*.). The problem might come from the `t` datasset. Could you please add the console output of `dput(head(t))` to your question? Or any other `t` dataset that mimics the error you encounter. Read more about reproducible example here: https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – Paul Apr 29 '22 at 10:58
  • Are you sure that all the output from `dput(head(t))` is there? When I copy-paste it to recreate the object I get the error `Error: unexpected symbol in "structure(list(year = c(1949L, ... class"` – Paul Apr 29 '22 at 11:17
  • No i cropped it because the interior was the same type as the year=( – Inès BEAUMONT Apr 29 '22 at 11:27
  • The `dput(head))` command creates a code that anyone can copy-paste to create the exact same `t` object you are using. Please do not crop it, it is ok if it is a bit long. – Paul Apr 29 '22 at 11:29
  • Hello, ok and sorry for the time, i tried this command which works but when i compile it on an empty file I have a mistake : Erreur : '<' inattendu(e) in: "7, 4.4, 3.2, 0.9, 7.3, 5.3), `Tmoy_TOULOUSE-BLAGNAC` = c(5.8, 6, 2.5, 3.1, 7.7, 7.7), Tmoy_TOURS = c(NA_real_, NA_real_,NA_real_, NA_real_, NA_real_, NA_real_)), row.names = c(NA,-6L), class = c("data.table", "data.frame"), .internal.selfref = <" Which translates to error < excpected in : ... – Inès BEAUMONT May 02 '22 at 14:57
  • @InèsBEAUMONT I am not sure to understand what you have done. If "this" command is `dput()` then you do not need to compile something on an empty file. Just **copy-paste** en entire console output of `dput(head(t))`. – Paul May 03 '22 at 06:11
  • Ex: `dput(head(iris))`, gives `structure(list(Sepal.Length = c(5.1, 4.9, 4.7, 4.6, 5, 5.4), Sepal.Width = c(3.5, 3, 3.2, 3.1, 3.6, 3.9), Petal.Length = c(1.4, 1.4, 1.3, 1.5, 1.4, 1.7), Petal.Width = c(0.2, 0.2, 0.2, 0.2, 0.2, 0.4), Species = structure(c(1L, 1L, 1L, 1L, 1L, 1L), levels = c("setosa", "versicolor", "virginica"), class = "factor")), row.names = c(NA, 6L), class = "data.frame")` – Paul May 03 '22 at 06:11
  • Hello, I just added the result of dput(head(t)) in my question. Some columns begin and end with this character ` but not all of them. It doesn't seem to appear in the edit. – Inès BEAUMONT May 03 '22 at 09:42
  • The `` does not appear in the question because StackOverflow uses `` to format your text as code. Generally speaking, you want to avoid these specific characters (I mean minus, plus, *etc*.) in your column names. – Paul May 03 '22 at 14:54
  • 1
    As @Paul note in his answer, when you do `t[3:ncol(t)]` in `data.table` you are subsetting rows, not columns. Check section c) of `vignette("datatable-intro", package = "data.table")` – iago May 04 '22 at 07:25

2 Answers2

1

Short answer

When using data.table objects, it seems you cannot implicitely refer to columns if you do not use comma inside [].

library(data.table)
# with data.frame
colnames(iris[3:ncol(iris)])
#> [1] "Petal.Length" "Petal.Width"  "Species"
colnames(iris[,3:ncol(iris)])
#> [1] "Petal.Length" "Petal.Width"  "Species"
# with data.table
t <- as.data.table(iris)
colnames(t[3:ncol(t)]) # here you get the colnames of the rows 3 to 5
#> [1] "Sepal.Length" "Sepal.Width"  "Petal.Length" "Petal.Width"  "Species"
colnames(t[,3:ncol(t)])
#> [1] "Petal.Length" "Petal.Width"  "Species"

For your t object

I do not know how you have built the t object. I had to remove .internal.selfref = < pointer:0x0000022fd86f1ef0 >, from the output of dput(head(t)), to get a R object of classes data.table and data.frame. This object works fine with print(colnames(t[,3:ncol(t)])) (note the comma). I do not use data.table so I guess you need to use the syntax with the comma if you want to refer to columns of an object of class data.table.

    library(data.table)
    t <- structure(
      list(
        year = c(1949L, 1949L, 1949L, 1949L, 1949L, 1949L),
        month = c(1L, 1L, 1L, 1L, 1L, 1L),
        day = 1:6,
        `Tmoy_AGEN-LA GARENNE` = c(6.9, 5.9, 2.9, 2, 7.9, 7.2),
        Tmoy_AUXERRE = c(NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_),
        `Tmoy_BALE-MULHOUSE` = c(2.8, 1.3, 2.6, 1.6, 2.5, -0.7),
        Tmoy_BESANCON = c(7.6, 2, 3.6, 3.1, 4, 3.3),
        `Tmoy_BIARRITZ-PAYS-BASQUE` = c(NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_),
        `Tmoy_BORDEAUX-MERIGNAC` = c(9.4, 5.5, 6.2, 5.5, 8.9, 3.5),
        Tmoy_BOURGES = c(7.6, 4.2, 3.7, 3.1, 6.6, 6.2),
        `Tmoy_BREST-GUIPAVAS` = c(5.5, 5, 5.5, 7.8, 8.3, 8.9),
        `Tmoy_CHAMBERY-AIX` = c(NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_),
        `Tmoy_CLERMONT-FD` = c(6.3, 3.8, 5.4, 0.9, 5.5, 5.3),
        Tmoy_COGNAC = c(8.6, 4.8, 5.4, 5.3, 8.3, 5.6),
        `Tmoy_DIJON-LONGVIC` = c(5.7, 3.8, 3.9, 0.3, 4.5, 4.6),
        Tmoy_DINARD = c(NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_),
        `Tmoy_GRENOBLE-ST GEOIRS` = c(5.2, 5.3, -0.2, -0.9, 3.9, 1.2),
        `Tmoy_LILLE-LESQUIN` = c(5.4, 3.7, 2.8, 3.8, 6.1, 5.5),
        Tmoy_LUXEUIL = c(5.9, 1.6, 3.4, -0.6, 3, -1.5),
        `Tmoy_LYON-BRON` = c(6.6, 5, 4.2, 1, 5.7, 4.1),
        Tmoy_MARIGNANE = c(7.2,  12.3, 5.6, 3.2, 7, 6.3),
        `Tmoy_METZ-FRESCATY` = c(4.6, 3.9, 2.6, 1, 4, 4.3),
        Tmoy_MONTELIMAR = c(4.9, 9.8, 2.4, 4.6, 8.2, 7.6),
        `Tmoy_NANTES-BOUGUENAIS` = c(8.5, 4.9, 4.6, 6.2, 8.2, 5.6),
        Tmoy_NICE = c(7.6, 10.5, 7.2, 8.7, 7.6, 11.4),
        `Tmoy_NIMES-COURBESSAC` = c(6.2, 10.7, 4.9, 5, 8.9, 10.1),
        `Tmoy_PARIS-MONTSOURIS` = c(7.4, 4.4, 3.8, 3.6, 8.1, 6.4),
        `Tmoy_PAU-UZEIN` = c(8.3, 6.1, 4.7, 4.8, 9.4, 7.3),
        Tmoy_PERPIGNAN = c(8.1, 9.4, 6.5, 7.9, 9.6, 10.6),
        `Tmoy_REIMS-PRUNAY` = c(NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_),
        `Tmoy_ROUEN-BOOS` = c(NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_),
        `Tmoy_ST ETIENNE-BOUTHEON` = c(7, 4.4, 3.2, 0.9, 7.3, 5.3),
        `Tmoy_TOULOUSE-BLAGNAC` = c(5.8, 6, 2.5, 3.1, 7.7, 7.7),
        Tmoy_TOURS = c(NA_real_, NA_real_,  NA_real_, NA_real_, NA_real_, NA_real_)
      ),
      row.names = c(NA, -6L),
      class = c("data.table", "data.frame"),
      sorted = c("year", "month", "day")
    )

Output:

> colnames(t[,3:ncol(t)]) # note the addition of the comma
 [1] "day"                       "Tmoy_AGEN-LA GARENNE"      "Tmoy_AUXERRE"             
 [4] "Tmoy_BALE-MULHOUSE"        "Tmoy_BESANCON"             "Tmoy_BIARRITZ-PAYS-BASQUE"
 [7] "Tmoy_BORDEAUX-MERIGNAC"    "Tmoy_BOURGES"              "Tmoy_BREST-GUIPAVAS"      
[10] "Tmoy_CHAMBERY-AIX"         "Tmoy_CLERMONT-FD"          "Tmoy_COGNAC"              
[13] "Tmoy_DIJON-LONGVIC"        "Tmoy_DINARD"               "Tmoy_GRENOBLE-ST GEOIRS"  
[16] "Tmoy_LILLE-LESQUIN"        "Tmoy_LUXEUIL"              "Tmoy_LYON-BRON"           
[19] "Tmoy_MARIGNANE"            "Tmoy_METZ-FRESCATY"        "Tmoy_MONTELIMAR"          
[22] "Tmoy_NANTES-BOUGUENAIS"    "Tmoy_NICE"                 "Tmoy_NIMES-COURBESSAC"    
[25] "Tmoy_PARIS-MONTSOURIS"     "Tmoy_PAU-UZEIN"            "Tmoy_PERPIGNAN"           
[28] "Tmoy_REIMS-PRUNAY"         "Tmoy_ROUEN-BOOS"           "Tmoy_ST ETIENNE-BOUTHEON" 
[31] "Tmoy_TOULOUSE-BLAGNAC"     "Tmoy_TOURS"   
Paul
  • 2,850
  • 1
  • 12
  • 37
0
t <- mtcars
colnames(t[3:ncol(t)])

"disp" "hp"   "drat" "wt"   "qsec" "vs"   "am"   "gear" "carb"
Julian
  • 6,586
  • 2
  • 9
  • 33
  • Hello, i tried str(t) which gives this : Classes ‘data.table’ and 'data.frame': 25702 obs. of 34 variables: $ year : int 1949 1949 1949 1949 1949 1949 1949 1949 1949 1949 ... ... - attr(*, ".internal.selfref")= - attr(*, "sorted")= chr [1:3] "year" "month" "day" – Inès BEAUMONT Apr 29 '22 at 11:09
  • 1
    Seems like it does not work with `data.table` objects but you could not know because at the time the data was not shared. – Paul May 04 '22 at 07:07