我想使用另一列作为参考,从较大的表中为一个列分配一个值。
E.g。数据:
require(data.table)
dt <- data.table(N=c(1:5),GPa1=c(sample(0:5,5)),GPa2=c(sample(5:15,5)),
GPb1=c(sample(0:20,5)),GPb2=c(sample(0:10,5)),id=c("b","a","b","b","a"))
N GPa1 GPa2 GPb1 GPb2 id
1: 1 4 10 7 0 b
2: 2 5 15 19 7 a
3: 3 1 5 20 5 b
4: 4 0 13 3 4 b
5: 5 3 7 8 1 a
我们的想法是获得新列Val1
和Val2
。以1结尾的任何GP
列都有资格获得Val1
,任何2结尾都有资格获得Val2
。要插入到列中的值由每行id
列确定。
因此,您可以看到Val1
,您会在GPb1
列,然后GPa1
,GPb1
,GPb1
上再次绘制,最后GPa1
1}}。
最终结果将是;
N GPa1 GPa2 GPb1 GPb2 id Val1 Val2
1: 1 4 10 7 0 b 7 0
2: 2 5 15 19 7 a 5 15
3: 3 1 5 20 5 b 20 5
4: 4 0 13 3 4 b 3 4
5: 5 3 7 8 1 a 3 7
我确实得到了答案,但在熔化之后有很多行等等,但我确信在data.table
中必须有一种优雅的方式来做到这一点。我最初感到沮丧的是paste0
无法在data.table
;
dt[1,paste0("GP",id,"1")]
# The following gives a vector that is correct for Val1 (and works for 2)
diag(as.matrix(dt[,.SD,.SDcols=dt[,paste0("GP",id,"1")]]))
# I think the answer lies in `set`, but i've not had any luck.
for (i in 1:nrow(dt)) set(dt, i=dt[i,.SD,.SDcols=dt[,paste0("GP",id,"2")]], j=i, value=0)
但;
$('#myForm').validate({
rules: {
aiterms: {
required: true
}
},
submitHandler: function(form) {
$$.ajaxSetup({ cache: false });
var myterms;
var mybookingid;
$("#submit").unbind().click(function() {
myterms = $("#myterms").val();
mybookingid = $("#my_booking_id").val();
if ($('#myterms').is(":checked")==false)
{
myApp.alert('You need to accept terms to proceed further!', 'Terms Not Accepted!');
}
else
{
$$.ajax({
type: "POST",
url: "submit1.php",
dataType : 'html',
data: {myterms1: myterms,mybookingid1: mybookingid },
cache: false,
success: function(result){
$("#mycontent" ).empty();
$("#mycontent").html(result);
},//success
error: function(result)
{
myApp.alert('We are unable to process data due to technical reasons!', 'Error');
}
});
}//else
return false;
});
}//submit handler
}); //validate
这样的数据非常难看,所以也许最好只使用融化方法。
答案 0 :(得分:3)
dt[id == "a", c("Val1", "Val2") := .(GPa1, GPa2)]
dt[id == "b", c("Val1", "Val2") := .(GPb1, GPb2)]
# N GPa1 GPa2 GPb1 GPb2 id Val1 Val2
#1: 1 2 13 5 8 b 5 8
#2: 2 3 8 7 2 a 3 8
#3: 3 5 11 19 1 b 19 1
#4: 4 4 5 6 9 b 6 9
#5: 5 1 15 1 10 a 1 15