为什么在 R 中重新编码不会改变原始值? [英] Why is recode in R not changing the original values?

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

问题描述

我正在尝试在 R(来自 car 包)中使用 recode 但它不起作用.我将 .csv 文件中的数据读入名为 results 的数据框中.然后,我根据以下逻辑替换 Built_year 列中的值.

I'm trying to use recode in R (from the car package) and it is not working. I read in data from a .csv file into a data frame called results. Then, I replace the values in the column Built_year, according to the following logic.

recode(results$Built_year, 
       "2 ='1950s';3='1960s';4='1970s';5='1980s';6='1990s';7='2000 or later'")

当我在完成这一步后检查 results$Built_year 时,它似乎已经奏效了.但是,它不存储此值,而是返回其先前的值.我不明白为什么.

When I check results$Built_year after doing this step, it appears to have worked. However, it does not store this value, and returns to its previous value. I don't understand why.

谢谢.

(目前出了点问题,我看不到任何格式化图标)

(at the moment something is going wrong and I can't see any of the icons for formatting)

推荐答案

您需要分配给一个新变量.

car 包中的 recode 为例

R> x <- rep(1:3, 3)
R> x
[1] 1 2 3 1 2 3 1 2 3
R> newx <- recode(x, "c(1,2)='A'; else='B'")
R> newx
[1] "A" "A" "B" "A" "A" "B" "A" "A" "B"
R> 

顺便说一下,这个包叫car,而不是cars.

By the way, the package is called car, not cars.

这篇关于为什么在 R 中重新编码不会改变原始值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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