dat<- structure(list(
id = c("134235", "23459", "165432"),
name = c("dave", "mary", "jane"),
links = c("'34657', '34563', '23459'",
"'134235', '45868', '45677'",
"'134235', '23459', '44657'")),
.Names = c("id", "name", "links"),
row.names = c(NA, -3L), class = "data.frame")
# It can all be done in base, of course...
library(stringr)
library(reshape2)
# This would be easy to do if links weren't in that format -
# one record per id-link pair would be preferable.
# Split dat$links and remove any quotes
dat.wider <- data.frame(
dat[ , c("id", "name")],
str_split_fixed(string = gsub(dat$links,
pattern = "['|\"]",
replace = ""),
pattern = ", ",
n = 3)
)
# Reshape
dat.long <- melt(dat.wider, id.var = c("id", "name"))
# Self-join - this is not quite the right method, but I'm just not
# thinking straight right now
dat.joined <- unique(merge(x = dat.long[ , c("name", "value")],
y = dat.long[ , c("id", "name")],
by.x = "value",
by.y = "id"
))
# And, finally, if you wanted vector output...
res <- with(dat.joined, paste(name.x, name.y, sep = ", "))
<驚呆了......那麼這真是太棒了!我知道R很強大,但是這讓它進入了另一個聯盟。所以我可以從這個mapply中學習如何運行函數conns,在每一行tab上做一些名字和鏈接(從這裏返回的是未列出的和未命名的)。在每個「鏈接」的conns中,您分割並查看每個id是否在其中。如果是這種匹配是用來返回名稱。 – mhawksey 2012-02-02 08:58:53
mhawksey,令人印象深刻的使用單個子句中的'i'開頭的所有兩個字母單詞(好的,您使用變量'id',但它是我使用谷歌搜索找到的最接近的例子之一)。 – Kylos 2014-01-09 19:42:02