Error Accessing KeyValue pair in R [duplicate] - r

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"

Related

Use dollar $ sign to access a column [duplicate]

This question already has answers here:
Dollar sign before a variable
(3 answers)
Dynamically select data frame columns using $ and a character value
(10 answers)
Closed 1 year ago.
This question may seem stupid, but I am curious about how to access a column by a column name with quote in data.frame, such as:
col.name <- colnames(feature)[i] # feature is a data frame, i is the index
feature$col.name # but this returns NULL

Obtaining a single value from a table in R [duplicate]

This question already has an answer here:
How to extract counts as a vector from a table in R?
(1 answer)
Closed 3 years ago.
I have a dataframe: trainData. Running table(trainData$Survived) I obtain the following table.
No Yes
1062 490
How can I obtain just the integer value relative to the No column?
Running this:
pNo = table(trainData$Survived)['No']
I still obtain a table in pNo:
No
1062
While I would like to have only 1062! How to do that?
Use [[:
Example:
table(iris$Species)[["setosa"]]
# [1] 50
You got a named vector. If you don't want the name 'No', try the as.numeric() method to convert it:
pNo <- as.numeric(table(trainData$Survived)['No'])
pNo

How to get a list of all the different values within a variable [duplicate]

This question already has answers here:
Extracting unique rows from a data table in R [duplicate]
(2 answers)
Closed 4 years ago.
I've always wondered if there's a command in R that gives you the entire domain of values within a variable.
For example, let's say I have the following data.table:
dt
Household Number_of_children
1 0
2 3
3 3
Is there a command along the lines of summary() or str() that would return the list 0, 3?
I believe summary and str only do that when your variable is a character string. I don't know how to do this when your variable is an integer, numeric, etc.
You can use unique() function, with column/vector as input.
unique(dt[,'Number_of_children'])

R reference filtering column by variable name [duplicate]

This question already has answers here:
Dynamically select data frame columns using $ and a character value
(10 answers)
Closed 4 years ago.
Is there a way to get a filtered data frame, like this:
data[data$Measure=="Baseline",]
using a variable Name for Measure, i.e. measVarName == "Measure"?
Thanks.
Double bracket notation lets you select variables using a character string stored in a variable:
measVarName <- 'Measure'
data[data[[measVarName]] == 'Baseline',]

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]]

Resources