[问题] for循环问题

楼主: jasonfghx (工人)   2018-05-30 22:08:58
[问题类型]:
程式咨询(我想用R 做某件事情,但是我不知道要怎么用R 写出来)
[软件熟悉度]:
入门(写过其他程式,只是对语法不熟悉)
[问题叙述]:
for(j in 1:nrow(datasteel))
if (datasteel[j,28]==1)
{
datasteel[j,35] <- colnames(datasteel[,28])
}
for(j in 1:nrow(datasteel))
if (datasteel[j,29]==1)
{
datasteel[j,35] <- colnames(datasteel[,29])
}
for(j in 1:nrow(datasteel))
if (datasteel[j,30]==1)
{
datasteel[j,35] <- colnames(datasteel[,30])
}
....
for(j in 1:nrow(datasteel))
if (datasteel[j,34]==1)
{
datasteel[j,35] <- colnames(datasteel[,34])
}
我写成这样
for(i in 28:34)
{
for(j in 1:nrow(datasteel)){
if (datasteel[j,i]==1)
{
datasteel[j,35] <- colnames(datasteel[,i])
}}
}
Error in `[.data.table`(datasteel, j, i) :
j (the 2nd argument inside [...]) is a single symbol but column name 'i' is
not found. Perhaps you intended DT[, ..i]. This difference to data.frame is
deliberate and explained in FAQ 1.1.
有点不解, 想一日了自己解决不出来想请教可以怎么改善
谢谢回复
[程式范例]:
[关键字]:
作者: andrew43 (讨厌有好心推文后删文者)   2018-05-30 23:56:00
改成 colnames(datasteel)[i]另外,既然用了 data.table 推荐用 set() 来赋值还有改成 if (datasteel[j,..i]==1)你可以看看 vignette("datatable-faq") 的说明。
楼主: jasonfghx (工人)   2018-05-31 09:16:00
谢谢

Links booklink

Contact Us: admin [ a t ] ucptt.com