我有这个简单的问题要解决,我需要使用Stata
来解决。我对R
更熟悉,所以我开始在那里解决以确保我理解这个问题,然后想要翻译为Stata
。
针对类似问题的R代码
# Generate dummy data
n <- 30L
df <- data.frame(id = seq_len(n))
set.seed(10L)
question <- c("qa", "qb", "qc")
for (q in question) {
for (a in 1L:6L) {
df[, paste(q, a, sep = "_")] <- sample(c(0L:10L, NA),
size = n,
replace = TRUE)
}
}
# Now based on certain "activities" generate new variables
activity <- c(1, 4, 5, 6)
for (i in activity) {
# If answer to question c for that activity is equal to 3
# then value of the new variable is a multiple of the other two
vara <- paste0("qa_", i)
varb <- paste0("qb_", i)
varc <- paste0("qc_", i)
varn <- paste0("new", i)
df[, varn] <- as.numeric(rep(NA, times = n))
for (j in seq_len(n)) {
if (is.na(df[j, varc])) {
# Do nothing
} else if (df[j, varc] == 3) {
df[j, varn] <- df[j, vara] * df[j, varb]
}
}
}
我在Stata中测试过(玩过)
numlist "1 4 5 6"
global act `r(numlist)'
gen rows = _n
foreach i of global act {
di "qa_" + `i'
di "qb_" + `i'
di "qc_" + `i'
foreach j in rows {
di `j'
}
}
很难找到下一步。
答案 0 :(得分:2)
以下是Stata代码,它会在您的R代码中生成列public final MySingletonClass {
//pirivate final fields;
private MySingletonClass() {
//default initialization fields
}
public static MySingletonClass getInstance() {
return MySingletonClassSingletonHelper.INSTANCE;
}
private static class MySingletonClassSingletonHelper {
private static final MySingletonClass INSTANCE = new MySingletonClass();
}
//getters
}
,@Override
public Observable<MySingletonClass> loadData(Integer params) {
return mRetrofitInterface.getMySingletonClass(params).subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread());
}
public interface MySingletonClassService {
@POST("some address")
Observable<MySingletonClass> getMySingletonClass(@Path("idd") Integer idd);
}
,new1
,new4
。
new5
编辑(尼克考克斯):这似乎会减少到
new6
因为观察的内循环是非常不必要的,并且将文本放入本地只是为了非常快速地将其取出而没有任何意义。