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

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

Related

Complex string vector creation from data frame values [duplicate]

This question already has an answer here:
Convert R vector to string vector of 1 element [duplicate]
(1 answer)
Closed 2 years ago.
I have the following dataframe which has only one column called Values and a list of string values:
Values
AA
AB
CG
DS
KI
Is there a simple way to create a string vector with each value separated by |?
The desired resulting output should look something like this:
Vector = "AA|AB|CG|DS|KI"
Cheers!
Sure, you simply use (assuming your data is called df):
paste0(df$Values, collapse = "|")

R count number of variables with value ="mq" per row [duplicate]

This question already has answers here:
How to count the frequency of a string for each row in R
(4 answers)
Closed 4 years ago.
I have a data frame with 70variables, I want to create a new variable which counts the number of occurrences where the 70 variables take the value "mq" on a per row basis.
I am looking for something like this:
[ID] [Var1] [Var2] [Count_mq]
1. mq mq 2
2. 1 mq 1
3. 1 7 0
I have found this solution:
count_row_if("mq",DT)
But it gives me a vector with those values for the whole data frame and it is quite slow to compute.
I would like to find a solution using the function apply() but I don't know how to achieve this.
Best.
You can use the 'apply' function to count a particular value in your existing dataframe 'df',
df$count.MQ <- apply(df, 1, function(x) length(which(x=="mq")))
Here the second argument is 1 since you want to count for each row. You can read more about it from https://www.rdocumentation.org/packages/base/versions/3.5.1/topics/apply
I assume the name of dataset is DT. I'm a bit confused what you really want to get but this is how I understand. Data frame consists of 70 columns and a number of rows that some of them have observations 'mq'.
If I get it right, please see the code below.
apply(DT, function(x) length(filter(DT,value=='mq')), MARGIN=1)

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

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

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"

Resources