I have a p structure in R memory and I’m trying to access the Rate column of the matrix.
When I type p$6597858$Sample in the console, I get …
p$`6597858`$Sample
Rate Available X Y
[1,] 1.01 1520.93 0.00 0.0
[2,] 1.02 269.13 0.00 0.0
[3,] 1.03 153.19 0.00 0.0
[4,] 1.04 408.80 0.00 0.0
and so on …
Within my code when I try to
get("p$`6597858`$Sample[,1]")
I get this returned …
object 'p$`6597858`$Sample[ ,1]' not found
Is this an apostrophe problem?
Neither the
$nor the[[operator work withinget()(becausep[[1]]is not an R object, it’s a component of the objectp).You could try
but it’s probably a bad idea. Is there a reason that
doesn’t do what you want?
You can do this dynamically by using
charactervariables to index, still without usingget: for example(edit: suggested by Hadley Wickham in comments) or
(if the second-lowest-level element is a data frame or list: if it’s a matrix, this alternative won’t work, you would need
p[[c(as.character(needed),"Sample")]][,"Rate"]instead)This is a situation where figuring out the idiom of the language and working with it (rather than struggling against it) will pay off …
In general,
get()[[is more robust than$