从r中的长刺中提取朋友的ID

问题描述 投票:-1回答:1

我有一个包含数千行的数据框。数据框包含许多列,其中第三列在每个单元格中包含一个长字符串,如下所示。

> file_analysis$parameters[1]
[1] {"friends_id": [8396105, 20289687, 8222966], "opp_groups": [], "group_count": 752, "likes_count": 0, "friends_count": 3}
> file_analysis$parameters[2]
[1] {"friends_id": [7874795, 8093749], "opp_groups": [], "group_count": 69, "likes_count": 0, "posts_count": 0, "friends_count": 2}
> file_analysis$parameters[3]
[1] {"friends_id": [], "opp_groups": [], "group_count": 292, "likes_count": 0, "posts_count": 0, "friends_count": 0}

在几列中,朋友ID不存在。但是,在少数几列中,存在1个以上的朋友ID。如您所见,上面的三个单元格中,有以下朋友ID。

"friends_id": [8396105, 20289687, 8222966]
"friends_id": [7874795, 8093749]
"friends_id": []

我想从单独列中的每个单元格提取这些ID。在每次迭代中,我都希望有一个朋友ID的向量。此外,我想检查天气矢量是否为空。

我想要的输出如下;

id1 = (8396105, 20289687, 8222966)
id2 = (7874795, 8093749)
id2 = NULL
r regex substring rstudio extraction
1个回答
0
投票

首先,安装库。

library(qdapRegex)

然后,运行以下行。

> x = rm_between(file_analysis$parameters[1], "\": [", "]", extract=TRUE)[[1]][1]
  x
 [1] "8396105, 20289687, 8222966"

现在,进一步制作上述字符串的向量。

 y = unlist(strsplit(x, split=", "))
© www.soinside.com 2019 - 2024. All rights reserved.