R-将测试(以ifelse形式)分配在两个数字之间以生成虚拟变量

时间:2018-11-04 03:52:30

标签: r

当前代码:

[HttpPost]
        public JsonResult Listar2()
        {
            var search = Request.Form.GetValues("search[value]")?[0];

            var list = db.Students;

            if (!string.IsNullOrEmpty(search))
            {
                list = list.Where(m => m.name.ToLower().Contains(search.ToLower()) || m.class.ToLower().Contains(search.ToLower()));
            }

            var draw = Request.Form.GetValues("draw")?[0];
            var start = Request.Form.GetValues("start")?[0];
            var length = Request.Form.GetValues("length")?[0];

            var width = length != null ? Convert.ToInt32(length) : 0;
            var skip = start != null ? Convert.ToInt32(start) : 0;

            var totalRecords = list.Count();
            var resultFinal = list.Skip(skip).Take(width).ToList();

            return Json(new
            {
                data = resultFinal,
                draw,
                recordsFiltered = totalRecords,
                recordsTotal = totalRecords
            });
        }

使用上面的代码,我试图集中精力让data$light <- ifelse(data$cigs == 1:5, 1, 0) 仅将{{1}中的ifelse1的值1分配给5cigs是每天抽烟的香烟数量,范围从cigs0)。

1 个答案:

答案 0 :(得分:0)

尝试%in%

data$light <- ifelse(data$cigs %in% c(1:5), 1, 0)

或等效地

data$light <- ifelse(data$cigs >=1& data$cigs <=5, 1, 0)