我正在创建一个函数,该函数接收用户指定的单词列表,然后根据列表中数字的顺序将它们标记为数字。用户可以指定不同的列表长度。
例如:
myNotableWords<-c("No_IM","IM","LGD","HGD","T1a")
aa<-c("No_IM","IM","No_IM","HGD","T1a","HGD","T1a","IM","LGD")
aa<-data.frame(aa,stringsAsFactors=FALSE)
预期输出
new<-(1,2,1,4,5,4,5,2,3)
有没有办法获取原始列表的索引,然后查找目标列表中每个元素在该索引中的位置,然后将其替换为索引号?
答案 0 :(得分:1)
new <- c()
for (item in aa) {
new <- c(new, which(myNotableWords == item))
}
print(new)
#[1] 1 2 1 4 5 4 5 2 3
答案 1 :(得分:1)
您可以使用data.frame
执行此操作;语法不应更改。我更喜欢使用data.table
。
library(data.table)
myWords <- c("No_IM","IM","LGD","HGD","T1a")
myIndex <- data.table(keywords = myWords, word_index = seq(1, length(myWords)))
第三行只是向向量myWords
添加一个索引。
aa <- data.table(keywords = c("No_IM","IM","No_IM","HGD","T1a",
"HGD","T1a","IM","LGD"))
aa <- merge(aa, myIndex, by = "keywords", all.x = TRUE)
现在您有了一个显示关键字及其唯一编号的表。
答案 2 :(得分:1)
为什么不仅仅使用R的products_reservations
功能?
“因子数据类型”存储一个整数,该整数通过索引号引用“级别”(=字符串):
// ADD RESERVATION
$ins = $c->prepare("INSERT INTO products_reserved (uid,pid,timestamp) VALUES (?,?,?)");
$ins->bind_param('sss', $uid, $id, $timestamp);
$ins->execute();
$needid = $ins2->insert_id;
$timestampSeconds = strtotime($timestamp);
// SET INTERVAL FOR 6 HOURS
$delIntv = $c->prepare("
DELETE FROM products_reserved
WHERE ? = (? + INTERVAL 21600 SECOND)
AND id = ?
");
$delIntv->bind_param('sss', $timestampSeconds, $timestampSeconds, $needid);
$delIntv->execute();
if($ins && $delIntv) {
$c->commit();
}