作者jasonfghx (工人)
看板R_Language
标题[问题] for回圈问题
时间Wed May 30 22:08:58 2018
[问题类型]:
程式谘询(我想用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.
有点不解, 想一日了自己解决不出来想请教可以怎麽改善
谢谢回复
[程式范例]:
[关键字]:
--
※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 140.116.234.34
※ 文章网址: https://webptt.com/cn.aspx?n=bbs/R_Language/M.1527689340.A.342.html
1F:→ andrew43: 改成 colnames(datasteel)[i] 05/30 23:56
2F:→ andrew43: 另外,既然用了 data.table 推荐用 set() 来赋值 05/30 23:57
3F:→ andrew43: 还有改成 if (datasteel[j,..i]==1) 05/31 00:01
4F:→ andrew43: 你可以看看 vignette("datatable-faq") 的说明。 05/31 00:05
5F:→ jasonfghx: 谢谢 05/31 09:16