在 R 中重新编码数值 [英] Recode numeric values in R

查看:61
本文介绍了在 R 中重新编码数值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将一些数值重新编码为不同的数值并尝试使用以下代码:

I want to recode some numeric values into different numeric values and have had a go using the following code:

survey$KY27PHYc <- revalue(survey$KY27PHY1, c(5=3, 4=2,3=2,2=1,1=1))

我收到以下错误:

## Error: unexpected '=' in "survey$KY27PHYc <- revalue(survey$KY27PHY1, c(5="

我哪里出错了?

推荐答案

此函数不适用于数值向量.如果你想使用它,你可以这样做:

This function does not work on numeric vector. If you want to use it, you can do as follows:

 x <- 1:10 # your numeric vector

 as.numeric(revalue(as.character(x), c("2" = "33", "4" = "88")))

 # [1]  1 33  3 88  5  6  7  8  9 10

这篇关于在 R 中重新编码数值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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