Reputation: 61
I have an R data.frame object called x. From R console when I do str(x), following is what I get.
str (x)
'data.frame': 2776 obs. of 4 variables:
$ Date : Factor w/ 4018 levels "2003-01-01","2003-01-02",..: 1 6 11 16 21 26 31 36 41 46 ...
$ sulfate: num NA NA NA NA NA NA NA NA NA NA ...
$ nitrate: num NA NA NA NA NA NA NA NA NA NA ...
$ ID : int 1 1 1 1 1 1 1 1 1 1 ...
I now want to access values related to a particular variable. In that process when I do x.$nitrate
I get the values corresponding to that variable (ie nitrate) properly however when I do following
> frame_variable <- "nitrate"
and then do
> x$frame_variable I get NULL
This limitation is prohibiting me to use this feature in a function that I'm trying to create. Could somebody help here.
Thanks.
Upvotes: 0
Views: 1241
Reputation: 739
This is a way to access columns with a variable:
x[, frame_variable]
Upvotes: 3