有些示例将R中数据帧的单个向量中的值替换为其他值。例如 Replace a value in a data frame based on a conditional (`if`) statement in R& Replace numbers in data frame column in R?
我有一个问题的轻微变化,一个数据框(运动员)在DOB年有一些错误,我想通过这个并替换所有年份为00yy-mm-dd和19yy-mm的情况DD
我尝试athletes$DOB[athletes$DOB == 00]
初步对值进行了子集,但这不起作用
Name Country Height Weight Sex DOB Gold Silver Bronze
1 Lamusi A People's Republic of China 170 60 M 0089-06-02 0 0 0
2 A G Kruger United States of America 193 125 M 1979-02-18 0 0 0
3 Jamale Aarrass France 187 76 M 1981-11-15 0 0 0
4 Abdelhak Aatakni Morocco NA NA M 0088-02-09 0 0 0
5 Maria Abakumova Russian Federation 178 85 F 1986-01-15 0 0 0
6 Luc Abalo France 182 80 M 0084-09-06 0 0 0
答案 0 :(得分:1)
尝试sub
athletes$DOB <- as.Date(sub('^0{2}', '19', athletes$DOB))
athletes$DOB
#[1] "1989-06-02" "1979-02-18" "1981-11-15" "1988-02-09" "1986-01-15"
#[6] "1984-09-06"