问题描述
我进行了一项调查,其中用户按照李克特量表填写了问题的答案.当我想要的只是数字时,我们使用的软件已将文本放入答案中,因此示例是 NEITHER AGREE NOR DISAGREE4
.我想把它转换成4
I have a survey where users have filled in answers to questions on a likert scale. The software we are using has put text into the answers when all i want is numbers so an example is NEITHER AGREE NOR DISAGREE4
. I want to convert it to 4
我有执行此操作的 R 代码,它在数据框的单元格中查找第 18 列的数字,并从中提取数字(如果存在)
I have R code that does this where it looks for a number in the cell of the dataframe for column 18 and extracts the number from it if it exists
as.numeric(ifelse(grepl("[0-9]",df[,18]),as.numeric(gsub(x = df[,18],"[A-z]","")), df[,18]))
我希望能够为 cols 18:134
I would like to be able to do this for cols 18:134
我可以用 for 循环来做到这一点,我想,但我认为这是如何使用应用函数之一的完美示例,我想学习.
I can do this with a for loop, i guess but i think this is a perfect example of how to use one of the apply functions and i would like to learn.
谢谢
推荐答案
尝试
apply(df[,18:134], 2, function(x) gsub("[^0-9]", "", x))
这篇关于R:使用应用函数清理likert答案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!