如何在data.table列表中为函数分配多个列?

时间:2017-06-16 21:53:46

标签: r data.table

我有一个data.table列表,我想将一些列转换为数字并执行rowSums。我试图弄清楚如何将所有列转换为数字。

这就是我的尝试。

# Obtain data
tbl<-get_data(sqlquery = tqry, dbase=db1, server=serv)

# Names of the columns that need to be converted to numeric
score<-names(tbl)[grep('score',names(tbl),ignore.case = T)]
tbl[,class(AcceptingNewPatientsScore)]
[1] "character"

### Wrong - Having problem here
tbl[,eval(score):=as.numeric(get(score))] 

tbl[,class(AcceptingNewPatientsScore)]
[1] "numeric" # It converted but jumbled scores.

tbl[,tscore:=rowSums(.SD,na.rm = FALSE),.SDcols=score]

1 个答案:

答案 0 :(得分:1)

感谢@Frank的建议。

$(function(){

  var $nie = $('#nie');

  // Delegate the click event
  $(document).on('click', '.clicker' function(){

    var id = $(this).siblings('input').val();
    // Use id in upcoming AJAX request.
  });

  $.ajax({
    type: "POST",
    url: "../api/",
    data: {'data': "mydata"},
    dataType: 'JSON',
    success: function (res){

      $.each(res.response, function(i, r){

        $nie.appeand('<ul class="somec">\
        <li class="liclass">\
        <input value="'+ r.animal+ '" class="thatclass"/>\
        <a class="clicker"></a>\
        </li>\
        </ul>');
      });
    }
  });
});