我是R的新手,希望得到帮助
我想创建一个新的变量 lung.disease
,它应该是另外两个变量的平均值 COPD
和 emphysema2
.
如何去做呢?下面是数据的例子。
确实非常感谢
structure(list(current.age = c(68L, 67L, 71L, 65L, 62L), gender = c(0L,
0L, 1L, 0L, 1L), years.smoked = c(17L, 10L, 0L, 55L, 40L), years.quit = c(33L,
27L, 51L, 0L, 0L), cigs.day = c(10L, 20L, 5L, 60L, 12L), ethnicity = c(3L,
3L, 3L, 3L, 3L), COPD = c(0L, 0L, 0L, 0L, 0L), emphysema = c(NA,
NA, NA, 0L, 0L), LC.relative.numeric = c(0L, 0L, 0L, 0L, 1L),
BMI = c(29.1, 22.7, 32.1, 23.6, 26.8), education = c(1L,
1L, 1L, 1L, 1L), asbestos = c(0L, 0L, 0L, 0L, 0L), Pneumonia = c(0L,
0L, 0L, 0L, 0L), ever.cancer = c(0L, 0L, 0L, 0L, 0L), LC.relative.onset = c(0L,
0L, 0L, 0L, 2L), Dust = c(0L, 0L, 0L, 0L, 0L), LC.relative.multiple = c(0L,
0L, 0L, 0L, 0L), LC.relative.binary = c(0L, 0L, 0L, 0L, 1L
), Hay.Fever = c(0L, 0L, 0L, 0L, 0L), Asian.ethnicity = c(1L,
1L, 1L, 1L, 1L), Islander.ethnicity = c(0L, 0L, 0L, 0L, 0L
), American.indian.ethnicity = c(0L, 0L, 0L, 0L, 0L), Hypertension = c(NA_integer_,
NA_integer_, NA_integer_, NA_integer_, NA_integer_), CHDfromCVD = c(NA_integer_,
NA_integer_, NA_integer_, NA_integer_, NA_integer_), Angina = c(0L,
0L, 0L, 0L, 1L), Heart.attack = c(NA_integer_, NA_integer_,
NA_integer_, NA_integer_, NA_integer_), Other.Heart.Disease = c(NA,
NA, NA, NA, NA), Stroke = c(NA_integer_, NA_integer_, NA_integer_,
NA_integer_, NA_integer_), Diabetes = c(NA_integer_, NA_integer_,
NA_integer_, NA_integer_, NA_integer_), Bronchitis = c(NA,
NA, NA, NA, NA), Kidneys = c(0L, 0L, 0L, 0L, 0L), Liver = c(0L,
0L, 0L, 0L, 0L), Equipment = c(0L, 0L, 0L, 0L, 0L), Year.assessed = c(NA,
NA, NA, NA, NA), emphysema2 = c(0.618, 0.618, 0.618, 0, 0
)), row.names = c(NA, 5L), class = "data.frame")
这里有一些选项给你
df$lung.disease <- (df$COPD+df$emphysema2)/2
或
df$lung.disease <- rowMeans(df[c("COPD","emphysema2")])