how to use a quoted string in a t_test function [duplicate] - r

This question already has answers here:
Dynamically select data frame columns using $ and a character value
(10 answers)
How to write a loop to run the t-test of a data frame?
(5 answers)
Closed 3 years ago.
I have a test datafile
I would like to run a t test.
This will work, which I already know
t.test(df_test$clin_value ~ df_test$trt_variable)
But I would like to do this:
trt_var = "trt_variable"
noquote(trt_var) # which gives me trt_variable
why I can not run this?
t.test(df_test$clin_value ~ df_test$(noquote(trt_var)))
How can I make this work?
I have to do this way, because I would like to change trt_var constantly.

Related

Two sample T test in R [duplicate]

This question already has answers here:
How to perform a paired t-test in R when all the values are in one column?
(1 answer)
R - fast two sample t test
(2 answers)
Closed 1 year ago.
How do I run a T test comparing groups I and B by there accuracy? enter image description here
The command you are looking for is t.test(). In your case, it should look like:
t.test(accuracy ~ group, data = DATA_NAME)

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

combine objects of data frame in r [duplicate]

This question already has answers here:
Concatenate a vector of strings/character
(8 answers)
Closed 5 years ago.
I have a data frame like this:
I want to convert those objects to a long string like this, without the column's name salary:
23400;26800;9878;6754;7654;28907;6679
What should I do?
Assuming your data.frame is called x:
paste0(x$salary,collapse=';')

R, Add binary columns based on values in existing column [duplicate]

This question already has answers here:
Generate a dummy-variable
(17 answers)
Closed 5 years ago.
Beginner in R and looking to avoid unnecessary copy+pasting...
I have a data frame with a numeric column. I would like to create binary columns based on the values in the numeric column.
I know the tedious approach would be to copy+paste the following and manually add the different values:
DataFrame$NewCol1 <- as.numeric(DataFrame$ExistingCol == 1);
DataFrame$NewCol2 <- as.numeric(DataFrame$ExistingCol == 2);
Would a "for" loop be able to accomplish this task?
How about something like this?
model.matrix(~factor(DataFrame$ExistingCol))[,-1]

R Language: How to print the first or last rows of a data set? [duplicate]

This question already has answers here:
Extracting subset of the data frame in R
(4 answers)
Closed 8 years ago.
I can print first n rows by the following command:
> dataset[1:n, ]
Is there a more elegant way to do this? Also, how can I print the last rows of a data set?
If you want to print the last 10 lines, use
tail(dataset, 10)
for the first 10, you could also do
head(dataset, 10)

Resources