简体   繁体   中英

t.test in R for a specific column value (removing all that does not equal a certain row value)

I have a column in my data "TIME" which has one of three different values (It can be 000,007,014). I want to run t.test() on the data but only when TIME is 000.

Here is my try:

attach(data_all) # My data

t.test(CD4~SEX) # Column CD4 over column SEX

My question is, how can i specify to only include rows from when TIME column is 000 ? I have read the documentation, and looked up different blogposts and videos, but Have not found a way to do this. Any ideas?

EDIT:

Ive tried to use dplyr filter() like this:

data_all %>% filter(TIME != 000)

But its not filtering away all non 000 TIME rows

Try this. The first code will work with attached data:

#Code
t.test(CD4~SEX,subset = TIME=="000")

Also you can try:

#Code1
t.test(CD4~SEX,subset = TIME %in% c("000"))

And as an example:

#Code 2
t.test(extra ~ group, data = sleep,subset = ID %in% c('1','2'))

It will work.

In the case data is not attached, you can use:

#Code3
t.test(CD4~SEX,data=data_all,subset = TIME=="000")

We can also do

library(dplyr)
data_all %>%
  subset(TIME != "000") %>%
   t.test(CD4 ~ SEX, data = .)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM