Я понимаю, что этот вопрос уже задавался ранее, но я не могу заставить работать код.
Вот мои данные:
structure(list(ph503_3 = c(-1, -1, -1, 0, -1, -1), gripstrength = c(33,
40, 26, 30, 49, 31), IPAQmetminutes = c(5196, 198, 1674, 642,
11724, 1155), tugtimesec = c(8, 7, 7, 17, 9, 8), MHcesd = c(1,
0, 1, 12, 0, 9), id = c("292221", "334262", "075822", "40642",
"274222", "245801"), age = c(58, 68, 54, 64, 52, 58), COGmmse = c(30,
27, 29, 27, 30, 29), DISconverse1 = c("None", "None", "None",
"None", "None", "None"), MDantidepressant = c("No", "No", "No",
"Yes", "No", "No"), MDantipark = c(0, 0, 0, 0, 0, 0), MDpolypharmacy = c(0,
0, 0, 1, 0, 0), W2socialclass = c("Skilled", "Semi-skilled",
"Managerial & Technical", "Non-Manual", "Skilled", "Managerial & Technical"
), bh201 = c("Would never doze", "Would never doze", "Slight chance of dozing",
"Would never doze", "High chance of dozing", "Would never doze"
), fl001_01 = c("NOT Walking 100 metres (100 yards)", "NOT Walking 100 metres (100 yards)",
"NOT Walking 100 metres (100 yards)", "Walking 100 metres (100 yards)",
"NOT Walking 100 metres (100 yards)", "NOT Walking 100 metres (100 yards)"
), fl001_02 = c("NOT Running or jogging about 1.5 kilometres (1 mile)",
"Running or jogging about 1.5 kilometres (1 mile)", "NOT Running or jogging about 1.5 kilometres (1 mile)",
"Running or jogging about 1.5 kilometres (1 mile)", "Running or jogging about 1.5 kilometres (1 mile)",
"NOT Running or jogging about 1.5 kilometres (1 mile)"), fl001_04 = c("NOT Getting up from a chair after sitting for long periods",
"NOT Getting up from a chair after sitting for long periods",
"NOT Getting up from a chair after sitting for long periods",
"Getting up from a chair after sitting for long periods", "NOT Getting up from a chair after sitting for long periods",
"NOT Getting up from a chair after sitting for long periods"),
fl001_05 = c("NOT Climbing several flights of stairs without resting",
"NOT Climbing several flights of stairs without resting",
"NOT Climbing several flights of stairs without resting",
"Climbing several flights of stairs without resting", "NOT Climbing several flights of stairs without resting",
"NOT Climbing several flights of stairs without resting"),
fl001_06 = c("NOT Climbing one flight of stairs without resting",
"NOT Climbing one flight of stairs without resting", "NOT Climbing one flight of stairs without resting",
"Climbing one flight of stairs without resting", "NOT Climbing one flight of stairs without resting",
"NOT Climbing one flight of stairs without resting"), fl001_07 = c("NOT Stooping, kneeling, or crouching",
"NOT Stooping, kneeling, or crouching", "NOT Stooping, kneeling, or crouching",
"Stooping, kneeling, or crouching", "NOT Stooping, kneeling, or crouching",
"NOT Stooping, kneeling, or crouching")), row.names = c(NA,
-6L), class = c("tbl_df", "tbl", "data.frame"))
Мой код:
mydata <- mydata %>%
mutate_if(class(.)=="character" & str_detect(colnames(.), "^fl\\d|^ph\\d"), ~if_else(grepl("NOT ", .), 0, 1))
Код выполняется, но ничего не происходит, и я получаю следующее сообщение, когда связываю уценку:
Warning message:
In class(.) == "character" & str_detect(colnames(.), "^fl\\d|^ph\\d") :
longer object length is not a multiple of shorter object length