A Vajra
A Vajra

Reputation: 11

changing variable value in data frame

I have a data frame:

id,male,exposure,age,tol
9,0,1.54,tol12,1.79
9,0,1.54,tol13,1.9
9,0,1.54,tol14,2.12
9,0,1.54,tol11,2.23

However, I want the values of the age variable to be (11,12,13,14) not (tol11,tol12,tol13,tol14). I tried the following, but it does not make a difference.

levels(tolerance_wide$age)[levels(tolerance_wide$age)==tol11] <- 11
levels(tolerance_wide$age)[levels(tolerance_wide$age)==tol12] <- 12

Any help would be appreciated.

(data from Singer, Willett book)

Upvotes: 1

Views: 495

Answers (1)

pogibas
pogibas

Reputation: 28309

Assuming that you data frame is named foo:

foo$age <- as.numeric(gsub("tol", "", foo$age))

   id male exposure age  tol
1:  9    0     1.54  12 1.79
2:  9    0     1.54  13 1.90
3:  9    0     1.54  14 2.12
4:  9    0     1.54  11 2.23

Here we use two functions:

  • gsub to replace pattern in a string (we replace tol with nothing "").
  • as.numeric to transform gsub output (which is character) into numbers

Upvotes: 2

Related Questions