1

If I use df$columnName, you only get a vector which does not have the name of the column anymore. This means names(df$columnName) --> null

I could use df["columnName"], but this is kind of unhandy.. because I have to pass the columnName as string character.

aynber
  • 22,380
  • 8
  • 50
  • 63
Marcel Gangwisch
  • 8,856
  • 4
  • 23
  • 32
  • 2
    You are actually extracting different objects with `$` vs. `[`. `$` extracts the column as a vector, while `[` extracts a new `data.frame` with 1 column. Are you looking for a vector with a name attached? Because that will not be possible. – Julian_Hn Sep 29 '20 at 09:38
  • 2
    You can assign the column name to a string and then use that string in `df[columnNameString]`. Note, however, that this does not return a column, it returns a sub-df, therefore with a name. To return the column, use `df[[columnNameString]]`, with the same output as `df$columnName`. – Rui Barradas Sep 29 '20 at 09:39
  • 2
    See [Difference between `[` and `[[`](https://stackoverflow.com/questions/1169456/the-difference-between-bracket-and-double-bracket-for-accessing-the-el) and [Dynamically select data frame columns using $ and a vector of column names](https://stackoverflow.com/questions/18222286/dynamically-select-data-frame-columns-using-and-a-vector-of-column-names). – Rui Barradas Sep 29 '20 at 09:40
  • Yes, I was looking for something like a vector with a name attached to it.. but I dont care if it is a df. as type. – Marcel Gangwisch Sep 29 '20 at 09:41
  • @RuiBarradas hm yes this is an option.. but I can not extract the name from the column with the $-operator – Marcel Gangwisch Sep 29 '20 at 09:42
  • @MarcelGangwisch No you can't extract the name with `$`, since you are extracting a vector, which can't have a name attached. It can only have names for each of its elements. Depending on your further use, it might be easier to extract the names separately. – Julian_Hn Sep 29 '20 at 09:44
  • 1
    If you have to use a name and you can't use a string, you can go with `subset(df,,columnName)`. The double comma is intentional. – Edo Sep 29 '20 at 09:46
  • @Edo but then I have to use the string here in the subset as parameter.. – Marcel Gangwisch Sep 29 '20 at 09:48
  • @MarcelGangwisch in which usecase does the string bother you here? The only case I can think of is if you actually have to type it down. Are the two `"` really such a bother? – Julian_Hn Sep 29 '20 at 09:50
  • 4
    in subset `columnName` is a name not a string. But I think I'm missing the point..maybe we need to have more details on what you need that for or what exactly defines "best" for you [time-efficiency?] – Edo Sep 29 '20 at 09:52
  • @Edo thank you for your answer! I think your solution is exactly what I wanted. – Marcel Gangwisch Sep 29 '20 at 09:59
  • alternatively: `dplyr::select(df, columnName)` – Edo Sep 29 '20 at 10:00

2 Answers2

1

I prefer dplyr's select().

library('dplyr')   
data = df %>% select(columnName)

returns a one column dataframe.

safex
  • 2,398
  • 17
  • 40
0

Forming @Edo's great comment in an answer, we may use subset.

subset(x=dat, subset=, select=X1)

or short:

subset(dat,,X1)
#   X1
# 1  1
# 2  2
# 3  3

Data:

dat <- structure(list(X1 = 1:3, X2 = 4:6, X3 = 7:9, X4 = 10:12), class = "data.frame", row.names = c(NA, 
-3L))
jay.sf
  • 60,139
  • 8
  • 53
  • 110