I foresee this question will have to be heavily edited by a more experienced R user as I'm unsure of the right terminologies to use.
Here is a reproducible dataframe. Would it be possible to obtain the object from global environment and slice it like a normal dataframe?
df1 <- data.frame(fruit=c("apple", "Orange", "Pear"), location = c("Japan", "China", "Nigeria"), price = c(32,53,12))
df1
fruit location price
1 apple Japan 32
2 Orange China 53
3 Pear Nigeria 12
Obtaining list of objects in global environment
allobj <- ls()
allobj[1]
"df1"
I understand that by using the noquote
function, it returns the name of the object, which in this case is df1
- the name of my dataframe.
How do I treat this output as a named dataframe, in a base R slice? For instance,
(noquote(allobj[1]))[,1] #subset out only the 'fruit' column.
The above returns the error:
Error in unclass(x)[...] : incorrect number of dimensions
Is there a workaround for this?