我试图将哈希值复制到r。中的数据框 我在r中使用哈希包来构造我的哈希: Link to hash package
我已经能够使用以下代码将日期插入到哈希值中:
hash_test <- read.delim("hash_test.txt")
sizeH = nrow(hash_test)
h = hash()
for(i in 1:sizeH){
w = hash_test[i,4]
x = hash_test[i,5]
value = hash_test[i,]
hkey = paste(w, x, sep = "")
.set( h, keys=hkey, values=value )
}
我正在尝试使用以下方法将密钥复制到数据框中:
h_vals = keys(h)
size = length(h_vals)
h_new = rep(0,size)
for(i in 1:size){
current_key = h_vals[i]
h_new[i] = h[[current_key]]
}
但这并不是复制给定键的所有值。如果我尝试访问不变的密钥,它可以正常工作:
h[["Ube2d2GCAC"]]
LN.Tfr.1 LN.Tfr.2 LN.Tfr.3 Gene UMI
40.693 12.884 22.59467 Ube2d2 GCAC
以下是我用来测试的一些示例数据。实际数据要大得多:
LN.Tfr.1 LN.Tfr.2 LN.Tfr.3 Gene UMI
27.129 25.324 19.49333333 Tubgcp6 GCCC
8.887 8.886 5.924333333 Tubgcp6 GCCC
4.21 14.661 9.017 Uba52 GTTT
40.693 12.884 22.59466667 Ube2d2 GCAC
1.871 2.221 1.364 Ube2d3 GCAG
答案 0 :(得分:1)
以下是此问题的另一种解决方案:
h = hash()
for( i in 1:nrow(hash_test) ) {
w = hash_test[i,4]
x = hash_test[i,5]
z = paste(w, x, sep = "")
h[z] = i
}
提供者:Christopher Brown
答案 1 :(得分:0)
您可以尝试在代码中使用“as.character”功能。 当你使用as.character时,哈希表“h”可以识别一些字符而不是数字。
public ActionResult Create()
{
TicketVM model = new TicketVM();
ConfigureViewModel(model);
return View(model);
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(TicketVM model)
{
if (!ModelState.IsValid)
{
model.CategoryList = new SelectList(db.Categories, "CategoryID", "CategoryName");
ConfigureViewModel(model);
return View(model);
}
Ticket ticket = new Ticket
{
UserID = (int)WebSecurity.CurrentUserId,
Issue = model.Issue,
IssuedTo = model.IssuedTo,
CategoryID = model.CategoryID,
Priority = model.priority <-- ERROR
};
db.Tickets.Add(ticket);
db.SaveChanges();
return RedirectToAction("Index");
}
private void ConfigureViewModel(TicketVM model)
{
IEnumerable<User> admins = db.Users.Where(u => u.IsAdministrator).OrderBy(u => u.LastName);
model.AdministratorList = admins.Select(a => new SelectListItem
{
Value = a.UserID.ToString(),
Text = string.Format("{0} {1}", a.FirstMidName, a.LastName)
});
IEnumerable<Category> categories = db.Categories.OrderBy(u => u.CategoryName);
model.CategoryList = categories.Select(a => new SelectListItem
{
Value = a.CategoryID.ToString(),
Text = a.CategoryName.ToString()
});
}
祝你好运