Very New to R. Trying to create a variable

Hello, I'm very new to R and need some general help to get started in R studio.

I need to create a new variable called cd4cat defined as 0 if cd4< 100, 1 if cd4 ∈ [100, 199]
and 2 if cd4 ≥ 200. I've been told to literally type in create a new variable named cd4cat and it just got a big red X next to it so any assistance is appreciated. Below is where I'm at so far.

library(speff2trial)
library(survival)
data(ACTG175)

?ACTG175

Then define:

ACTG175$cd4cat[ACTG175@cd40<100]=0
ACTG175$cd4cat[ACTG175@cd40>=100 & ACTG175@cd40<=199]=1
ACTG175$cd4cat[ACTG175@cd40>=200]=2

You got the hard part right.

suppressPackageStartupMessages({
  library(speff2trial)
  library(survival)
})

data(ACTG175)
colnames(ACTG175)
#>  [1] "pidnum"  "age"     "wtkg"    "hemo"    "homo"    "drugs"   "karnof" 
#>  [8] "oprior"  "z30"     "zprior"  "preanti" "race"    "gender"  "str2"   
#> [15] "strat"   "symptom" "treat"   "offtrt"  "cd40"    "cd420"   "cd496"  
#> [22] "r"       "cd80"    "cd820"   "cens"    "days"    "arms"

ACTG175$cd4cat[ACTG175$cd40 < 100] <- 0
ACTG175$cd4cat[ACTG175$cd40 >= 100 & ACTG175$cd40 <= 199] <- 1
ACTG175$cd4cat[ACTG175$cd40 >= 200] <- 2

head(ACTG175["cd4cat"])
#>   cd4cat
#> 1      2
#> 2      1
#> 3      2
#> 4      2
#> 5      2
#> 6      2

but got tripped using the @ operator in place of $

ACTG175$cd4cat[ACTG175@cd40<100]=0
Error in ACTG175$cd4cat[ACTG175@cd40 < 100] = 0 : 
  trying to get slot "cd40" from an object (class "data.frame") that is not an S4 object 

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.