0

Probably a fairly simple question, but couldn't find anything specific for it. Lets say I have the following data frame

A
     Hello World
   1   5     6
   2   5     6

If you want to fetch the the values from the Hello column you would usually just do it with

A$Hello

But is there a way to fetch the column values with a predefined variable like this?

col <- "Hello"
A$col

The latter returns NULL for me

0

2 Answers 2

1
# A tibble: 2 x 2
  hello world
  <dbl> <dbl>
1     5     6
2     5     7
> col = 'hello'
> A[[col]]
[1] 5 5
Sign up to request clarification or add additional context in comments.

Comments

1

Try A[col]:

## Hello
##   <dbl>
## 1     5
## 2     5

Essentially, just uses another form of indexing. This works because col is a string.

2 Comments

this was useful, but not using double bracket syntax actually yields a table rather than a numerical vector like A$Hello does.
that's true - sorry I didn't completely think through what you might want (i.e. data frame versus vector)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.