r - Link id with name -


this question has answer here:

i know quite simple question i'm still quite new r.

if have 2 tables this

id = c(1,2,3,4,5,6) cost = c(11,22,33,44,55,66) name =c("aa","bb","cc","dd","ee","ff")  tableone = cbind (id, cost) tabletwo = cbind(name,id) 

how can add correct names (as in tabletwo) tableone costs? in reality table1 more complex multiple duplications etc.

here's way using data.table package:

load package:

library(data.table) 

load vectors had them defined:

id <- c(1,2,3,4,5,6) cost <- c(11,22,33,44,55,66) name <- c("aa","bb","cc","dd","ee","ff") 

turn vectors single column data tables:

id.dt<-data.table(id) setnames(id.dt,"id")  cost.dt<-data.table(cost) setnames(cost.dt,"cost")  name.dt<-data.table(name) setnames(name.dt,"name") 

combine columns, set key (this might unneeded example, included show:

tableone.dt<-cbind(id.dt,cost.dt) setkey(tableone.dt,id) tabletwo.dt<-cbind(name.dt,id.dt) setkey(tabletwo.dt,id) 

merge tables:

merge(tableone.dt,tabletwo.dt) 

of course round-about way of doing it. if suits needs can simplify process. example:

simple.table <- data.table("id"=c(1,2,3,4,5,6),"cost"=c(11,22,33,44,55,66),"name"=name <- c("aa","bb","cc","dd","ee","ff")) 

or thomas's comment work. also, see link commented flodel.


Comments

Popular posts from this blog

java - Jmockit String final length method mocking Issue -

asp.net - Razor Page Hosted on IIS 6 Fails Every Morning -

c++ - wxwidget compiling on windows command prompt -