Get element in list with numeric name [duplicate] - r

This question already has answers here:
Dynamically select data frame columns using $ and a character value
(10 answers)
Closed 3 years ago.
That's probably a duplicate, but I couldn't find the answer anywhere...
Anyways, here's my simple problem. I am loading a yaml file in R with users metadata. After parsing the yaml with yaml::yaml.load_file, the list looks like this:
$users
$users$`1`
$users$`1`$user
[1] "Alice"
$users$`2`
$users$`2`$user
[1] "Bob"
I can get the user with id = 1 by:
user_list$users$`1`$user
That returns:
[1] "Alice"
My question is: how can I transform a numeric 1 into this "quoted" version, so I can pass the id as a function argument that retrieves the user I want?

Instead of using $, use [[
id <- 1
user_list$users[[as.character(id)]]$user

Related

Using a character vector to refer to a data frame [duplicate]

This question already has answers here:
Access variable value where the name of variable is stored in a string
(4 answers)
Closed 2 years ago.
I want to refer to a data frame by using a character vector.
I believe the simple example below illustrates the problem.
# I have a data frame called A
A <- c(1, 2, 3, 4)
# I have a character vector called B, containing the character "A"
B <- "A"
# Now I want a third vector (C) to get the content of A, simply by referring to vector B
# Obviously, I cannot write
C <- B
# ... as this would give me
[1] "A"
# ... and NOT what I want:
[1] 1 2 3 4
How do I use a character vector to refer to the name and thus the content of an existing data frame?
PS.
I have been made aware of my questiong being a duplicate. But since the wordings are different, I didn't find the other post when searching online:
Access variable value where the name of variable is stored in a string
I keep my post, as others too might fail to find the earlier one.
It would be get to return the value of the object name as string
C <- get(B)
If there are more than objects, use mget to return the values in a list

Subsetting columns by a string variable returns NULL in R [duplicate]

This question already has answers here:
Dynamically select data frame columns using $ and a character value
(10 answers)
Closed 5 years ago.
I can subset a column named A from a data frame x_data using x_data$A.
But if I do
some_string<-'A'
x_data$some_string
I get NULL.
Can someone please explain why is it so. Thanks.
You can reference a data frame with the $ operator using a string literal, only a column. If you want to subset using a string, use the list syntax:
sub_df <- x_data[[some_string]]

Why names(a[2])<- "e" not working, but names(a)[2]<-"e" is working in R Language [duplicate]

This question already has an answer here:
Naming list elements in R
(1 answer)
Closed 5 years ago.
i tried Naming the Vector in R.
a<-1:5
names(a[2])<-"e" #this is not showing any warning or error but Naming is not done
but
names(a)[2]<-"e" # this is is Naming the Element properly.
Kindly help with Explanation.
The basic difference is in understanding what a[1] and names(a)[1] stands for.
a<-1:5
(This assigns values 1 to 5 and creates a vector)
a[1] # This gives below output i.e the value stored at first location
[1] 1
names(a)[1] # Shows the label associated with the value in this case 'NULL' yet
NULL
Now assigning the required name to the value
names(a)[2]<-"e"
This does the correct assignment and is how the R expects the code. You can then extract the value by element name namely
a["e"] #will give output
2

Error Accessing KeyValue pair in R [duplicate]

This question already has answers here:
Dynamically select data frame columns using $ and a character value
(10 answers)
Closed 6 years ago.
I am creating KeyValue pair as below:
Market <- c("ESA", "CLA", "GCA", "DXA")
Market_ID <- c(11,13,14,17)
MI_KV <- setNames(as.list(Market), Market_ID)
MI<-13
When I do the following, I get the desired output:
> MI_KV$`13`
[1]"CLA"
But When I do the following, I get NULL value as output:
> MI_KV$MI
NULL
How do I retrieve the value using the above command as MI will be dynamic?
We can use [[ and make sure to convert the object to character
MI_KV[[as.character(MI)]]
#[1] "CLA"

Capture last output as an R object [duplicate]

This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
How to assign the result of the previous expression to a variable in R?
I'm working with R in ESS and just made the stupid mistake of running a long running function without assigning the result to a variable. So, it just printed out the result, a long string of output that looks like:
[[1]]
1 FALSE
[[2]]
1 TRUE
[[3]]
1 TRUE
[[4]]
1 TRUE
Is there any way to coerce this printed output into an R object? Either within R, or using emacs (M-x undo-my-stupid-mistake)?
Maybe this will work:
out <- .Last.value

Resources