r - how do you merge two data frames the best way? -


i need merge data.frames:

dput(data)      structure(list(hostname = structure(c(8l, 8l, 9l, 5l, 6l, 7l,  1l, 2l, 3l, 4l), .label = c("db01", "db02", "farm01", "farm02",  "tom01", "tom02", "tom03", "web01", "web03"), class = "factor"),      date = structure(c(6l, 10l, 5l, 3l, 2l, 1l, 8l, 9l, 7l, 4l     ), .label = c("10/5/2015 1:15", "10/5/2015 1:30", "10/5/2015 2:15",      "10/5/2015 4:30", "10/5/2015 8:30", "10/5/2015 8:45", "10/6/2015 8:15",      "10/6/2015 8:30", "9/11/2015 5:00", "9/11/2015 6:00"), class = "factor"),      cpubusy = c(31l, 20l, 30l, 20l, 18l, 20l, 41l, 21l, 29l,      24l), usedpercentmemory = c(99l, 98l, 95l, 99l, 99l, 99l,      99l, 98l, 63l, 99l)), .names = c("hostname", "date", "cpubusy",  "usedpercentmemory"), class = "data.frame", row.names = c(na,  -10l)) dput(cmdb)  structure(list(hostname = structure(c(8l, 8l, 9l, 5l, 6l, 7l,  1l, 2l, 3l, 4l), .label = c("db01", "db02", "farm01", "farm02",  "tom01", "tom02", "tom03", "web01", "web03"), class = "factor"),      app = structure(c(4l, 4l, 4l, 3l, 3l, 3l, 1l, 1l, 2l, 2l), .label = c("db",      "farm", "tom", "web"), class = "factor"), ha = structure(c(3l,      4l, 5l, 2l, 6l, 6l, 1l, 6l, 6l, 6l), .label = c("hadb02",      "hatom", "haweb01", "haweb02", "haweb03", "no ha host"), class = "factor"),      dr = structure(c(3l, 4l, 5l, 2l, 6l, 6l, 1l, 6l, 6l, 6l), .label = c("drdb01",      "drtom", "drweb01", "drweb02", "drweb03", "no dr host"), class = "factor"),      date = structure(c(6l, 10l, 5l, 3l, 2l, 1l, 8l, 9l, 7l, 4l     ), .label = c("10/5/2015 1:15", "10/5/2015 1:30", "10/5/2015 2:15",      "10/5/2015 4:30", "10/5/2015 8:30", "10/5/2015 8:45", "10/6/2015 8:15",      "10/6/2015 8:30", "9/11/2015 5:00", "9/11/2015 6:00"), class = "factor"),      cpubusy = c(31l, 20l, 30l, 20l, 18l, 20l, 41l, 21l, 29l,      24l), usedpercentmemory = c(99l, 98l, 95l, 99l, 99l, 99l,      99l, 98l, 63l, 99l)), .names = c("hostname", "app", "ha",  "dr", "date", "cpubusy", "usedpercentmemory"), class = "data.frame", row.names = c(na,  -10l)) 

i doing this:

  env<-unique(colnames(cmdb[,c(2:4)]))   env<-as.factor(env)   env<-droplevels(env)    for(en in env){          mergeddata <- merge(data, cmdb, by.x=c("hostname"),by.y=en,all=t)         ##do other stuff here based on if prod, ha or dr    } 

it looks line taking long time do:

mergeddata <- merge(data, cmdb, by.x=c("hostname"),by.y=en,all=t) 

is there other way merge 2 data frames based on different column names , include these based on each env:

app, env, date, cpubusy, usedpercentmemory 


Comments

Popular posts from this blog

How to debug "expected android.widget.TextView but found java.lang.string" in Android? -

php - mySQL problems with this code? -