【发布时间】:2014-05-30 13:27:15
【问题描述】:
我有一个具有这种结构的数据集:
区域1 区域2 区域3 1 10 5 5 2 8 10 8 3 13 15 12 4 3 17 11 5 17 9 6 12 15 7 4 8 18 9 1我需要:
项目 区域1 区域2 区域3 1 1 1 0 0 2 3 1 0 0 3 4 1 0 0 4 5 0 1 1 5 8 1 0 1 6 9 0 0 1 7 10 1 1 0 8 11 0 0 1 9 12 1 0 1 10 13 1 0 0 11 15 0 1 1 12 17 1 1 0 13 18 1 0 0计划是获得一个不同的项目列表,将每个区域作为自己的列连接,并将匹配项替换为 1,缺少的替换为 0;但我一定错过了 R 合并的一个关键点,删除了感兴趣的主列。任何意见是极大的赞赏!我更喜欢 R 解决方案,但下一步是研究 sqldf 包。
#read in data
regions <- read.csv("c:/data/regions.csv")
#get unique list of items from all regions
items <- na.omit(unique(stack(regions)[1]))
#merge distinct items with each region, replace matches with 1, missings with 0
merge.test <- merge(items,regions,by.x="values", by.y=c("region1"), all=TRUE)
【问题讨论】:
-
能否提供
dput的数据?
标签: r reshape data-manipulation