reshape - R - Transposing portions of a data frame -
reshape - R - Transposing portions of a data frame -
i have .dbf file exported arcgis 10.1 , need reorganize it. illustration of info is:
v1 v2 40.000000000000000 41.000000000000000 40.000000000000000 42.000000000000000 41.000000000000000 40.000000000000000 41.000000000000000 42.000000000000000 41.000000000000000 43.000000000000000 42.000000000000000 40.000000000000000 42.000000000000000 41.000000000000000 42.000000000000000 43.000000000000000 43.000000000000000 41.000000000000000 43.000000000000000 42.000000000000000
i need info in format there 1 row each unique value in first column, of corresponding values sec column appearing in row, example:
v1 v2 v3 v4 40.000000000000000 41.000000000000000 42.000000000000000 41.000000000000000 40.000000000000000 42.000000000000000 43.000000000000000 42.000000000000000 40.000000000000000 41.000000000000000 43.000000000000000 43.000000000000000 41.000000000000000 42.000000000000000
if can help me problem appreciate it. thanks!
you can split info frame using split
function on first column , utilize lapply
extract vectors:
dat = data.frame(x1=c(40, 40, 41, 41, 41, 42, 42, 42, 43, 43), x2=c(41, 42, 40, 42, 43, 40, 41, 43, 41, 42)) res <- lapply(split(dat, dat[,1]), function(d) c(d[1,1], sort(unique(d[,2])))) res # $`40` # [1] 40 41 42 # # $`41` # [1] 41 40 42 43 # # $`42` # [1] 42 40 41 43 # # $`43` # [1] 43 41 42
most prefer maintain info in format, can combine list matrix, right-padding vectors na
values:
max.len <- max(unlist(lapply(res, length))) do.call(rbind, lapply(res, function(x) { length(x) <- max.len ; x })) # [,1] [,2] [,3] [,4] # 40 40 41 42 na # 41 41 40 42 43 # 42 42 40 41 43 # 43 43 41 42 na
r reshape transpose reorganize
Comments
Post a Comment