新结构变量的数据表解决方案 [英] Data Table Solution To New Structured Variable

查看:75
本文介绍了新结构变量的数据表解决方案的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  data = data.frame( student = c(1,1,1,1,2,2,2,2,2,3,3,4,4,4, 4),
分数 = c(1,2,1,1,2,3,2,NA,3,NA,1,3,2,1),
drop = c(0,0,0,0,0,0,0,1,0,1,0,0,0,0),
WANT = c(1,2,1,1,2 ,3,3,4,3,4,1,3,3,3))

I我希望使用data.table解决方案创建数据框 data而不是 WANT。



规则是:


如果得分= 1,则WANT = 1(如果得分= 2,则WANT = 2,如果得分= 3,WANT = 3,如果下降= 1,则WANT = 4

如果在t = 2时得分并且在t + 1 = 1时得分还可以,但



如果在t时得分= 3,且以后的分数小于3,则将它们替换为
,将其替换为3。


of: 1-2-1-3-1应该是:1-2-1-3-3

  data2 = data.frame( student = c(1,1,1,1,2,2,2,2,2,3,3,4,4,4,4, 5,5,5,5),
分数 = c(1,2,1,1,2,3,2,NA,3,NA,1,3,2,1,1,3 ,NA,2),
drop = c(0,0,0,0,0,0,0,1,0,1,0,0,0,0, 0,0,0,0),
WANT = c(1,2,1,1,2,3,3,4,3,4,1,3,3,3,1,3 ,3,3))


解决方案

使用 data.table

 库(data.table)

#如果分数= 1,如果分数= 2,WANT = 1如果分数= 3,WANT = 2如果WANT = 3
setDT(data)[,w:=分数]

#如果在t = 3时得分小于3,则将其替换为3。
data [data [,.I [cummax(score)== 3L&得分< 3L],学生] $ V1,w:= 3L]

#它添加了学生'5',该人的NA值我希望用之前的非缺失NA值
数据填充[ ,w:= nafill(w, locf)]

#if drop = 1,WANT = 4
data [drop == 1L,w:= 4L]


data=data.frame("student"=c(1,1,1,1,2,2,2,2,3,3,4,4,4,4),
"score"=c(1,2,1,1,2,3,2,NA,3,NA,1,3,2,1),
"drop"=c(0,0,0,0,0,0,0,1,0,1,0,0,0,0),
"WANT"=c(1,2,1,1,2,3,3,4,3,4,1,3,3,3))

I have dataframe 'data' sans 'WANT' which is what I hope to create using a data.table solution.

The rules are:

if score = 1, WANT = 1 if score = 2, WANT = 2 if score = 3, WANT = 3, if drop = 1, WANT=4

if score at t = 2 and score at t+1 = 1 that is ok but

if score at t = 3 and score at any later scores are less than 3, they are replaced with 3.

that means a score series of: 1-2-1-3-1 should be: 1-2-1-3-3

    data2=data.frame("student"=c(1,1,1,1,2,2,2,2,3,3,4,4,4,4,5,5,5,5),
"score"=c(1,2,1,1,2,3,2,NA,3,NA,1,3,2,1,1,3,NA,2),
"drop"=c(0,0,0,0,0,0,0,1,0,1,0,0,0,0,0,0,0,0),
"WANT"=c(1,2,1,1,2,3,3,4,3,4,1,3,3,3,1,3,3,3))

解决方案

An option using data.table:

library(data.table)

#if score = 1, WANT = 1 if score = 2, WANT = 2 if score = 3, WANT = 3
setDT(data)[, w := score]

#if score at t = 3 and score at any later scores are less than 3, they are replaced with 3.
data[data[, .I[cummax(score)==3L & score < 3L], student]$V1, w := 3L]

#it add student '5' which has NA values that I hope to fill with prior non-missing NA value
data[, w := nafill(w, "locf")]

#if drop = 1, WANT=4
data[drop==1L, w := 4L]

这篇关于新结构变量的数据表解决方案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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