Stata:变量的元素 [英] Stata: Elements of variable
问题描述
如何编辑变量的每第二个值?
How can I edit every 2nd value of a variable?
我的代码是:
set obs 100
gen u = invnorm(uniform())
forvalues d = 1/50 {
gen u[2*d] = u[2*d] + 1
}
我的代码有什么问题?
推荐答案
generate
的语法除了存储类型和变量名(以及标签名,这里无关紧要)之外不允许任何东西generate
和 =
之前.帮助文件中明确指出了这一点.
The syntax for generate
doesn't allow anything except a storage type and variable name (and a label name, irrelevant here) after generate
and before =
. This is clearly indicated by the help file.
这里不需要循环.如果你想处理观察 2, 4, ... 那么
You don't need a loop here. If you want to work on observations 2, 4, ... then
gen new_u = u + 1 if mod(_n, 2) == 0
选择偶数观察数字.更改现有变量 generate
是非法的,但您可以使用
selects even observation numbers. To change an existing variable generate
is illegal, but you could go
replace u = u + 1 if mod(_n, 2) == 0
mod(_n, 2) == 0
是
!mod(_n, 2)
鉴于整数除以 2 的模数(严格来说,余数)只能是 1 或 0,所以否定零(逻辑假)给你一个(逻辑真).
given that the modulus (strictly, remainder) on dividing integers by 2 can only be 1 or 0, so negating zeros (logical false) gives you ones (logical true).
附言
invnorm(uniform())
是一种用均值 0 和方差 1 调用随机正态偏差的古老方法.在现代 Statas 中
is an ancient way to call random normal deviates with mean 0 and variance 1. In modern Statas
rnormal()
会做的.
这篇关于Stata:变量的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!