我正在尝试从 Stata 翻译以下内容
clear
set obs 1000
generate y = floor((10-0+1)*runiform() +0)
recode y (7=0) (8=0) (9=1) (10=2)
我以为我有以下代码:
library(dplyr)
mydata <- y ~ floor((10-0+1)*runif(1000)+0)
recode (mydata, '7'=0, '8'=0, '9'=1, '10'=2)
但是,最后一行一直给我一个错误:
Error in UseMethod("recode"):no applicable method for 'recode' applied to an object of class "formula".
有什么想法吗?
最佳答案
你可以使用cut
:
n = 1000L
y = cut(runif(n, 0, 11), c(-Inf, 9, 10, Inf), right = FALSE, ordered = TRUE)
你可以看到它是如何与 table
一起工作的:
# y
# [-Inf,9) [9,10) [10, Inf)
# 813 91 96
如果你真的想要代码,你可以使用as.integer(y)-1L
。阅读 ?cut
和 ?factor
了解有关 R 中序数数据的更多详细信息。
到目前为止,我一直假设重新编码规则有一些韵律或原因。如果没有,最好将它存储在一个单独的表中并从那里提取值(这与我在 Stata 中所做的相同):
rec = data.frame(old = c(7,8,9,10), new = c(0,0,1,2))
n = 1000L
y = floor(runif(n, 0, 11))
DF = data.frame(id = 1:10, y)
library(data.table)
setDT(DF)
DF[rec, on=c(y = "old"), y := new]
DF[, .N, keyby=y]
# y N
# 1: 0 288
# 2: 1 179
# 3: 2 174
# 4: 3 101
# 5: 4 82
# 6: 5 93
# 7: 6 83
不过,您需要安装 data.table 包才能正常工作。
关于r - 将 Stata 翻译成 R - 重新编码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39209118/