R:使用Apply Function清理Likert答案 [英] R: Using Apply Function to clean likert answers

查看:86
本文介绍了R:使用Apply Function清理Likert答案的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我进行了一项调查,在调查中,用户以李克特量表填写了问题的答案.当我想要的只是数字时,我们正在使用的软件会将文本放入答案中,因此示例为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循环来做到这一点,但是我认为这是如何使用apply函数之一的完美示例,我想学习.

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:使用Apply Function清理Likert答案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆