【发布时间】:2018-10-18 13:44:48
【问题描述】:
我想从这个由 15 个观察值组成的向量中提取数字:
rs <- c("\n \n \n \n\n \n \n \n\n \n \n \n \n \n \n \n \n \n \n 4.0\n (1 rating)\n \n \n \n Course Ratings are calculated from individual students’ ratings and a variety of other signals, like age of rating and reliability, to ensure that they reflect course quality fairly and accurately.\n \n \n \n \n \n \n\n\n \n \n \n\n \n \n 9 students enrolled\n \n \n\n\n \n\n ",
"\n \n \n \n\n \n \n \n\n \n \n \n \n \n \n \n \n \n \n 4.7\n (4 ratings)\n \n \n \n Course Ratings are calculated from individual students’ ratings and a variety of other signals, like age of rating and reliability, to ensure that they reflect course quality fairly and accurately.\n \n \n \n \n \n \n\n\n \n \n \n\n \n \n 34 students enrolled\n \n \n\n\n \n\n ",
"\n \n \n \n\n \n \n \n\n \n \n \n \n \n \n \n \n \n \n 3.1\n (5 ratings)\n \n \n \n Course Ratings are calculated from individual students’ ratings and a variety of other signals, like age of rating and reliability, to ensure that they reflect course quality fairly and accurately.\n \n \n \n \n \n \n\n\n \n \n \n\n \n \n 22 students enrolled\n \n \n\n\n \n\n ",
"\n \n \n \n\n \n \n \n\n \n \n \n \n \n \n \n \n \n \n 2.4\n (14 ratings)\n \n \n \n Course Ratings are calculated from individual students’ ratings and a variety of other signals, like age of rating and reliability, to ensure that they reflect course quality fairly and accurately.\n \n \n \n \n \n \n\n\n \n \n \n\n \n \n 2,106 students enrolled\n \n \n\n\n \n\n ",
"\n \n \n \n\n \n \n \n\n \n \n \n \n \n \n \n \n \n \n 4.3\n (67 ratings)\n \n \n \n Course Ratings are calculated from individual students’ ratings and a variety of other signals, like age of rating and reliability, to ensure that they reflect course quality fairly and accurately.\n \n \n \n \n \n \n\n\n \n \n \n\n \n \n 1,287 students enrolled\n \n \n\n\n \n\n ",
"\n \n \n \n\n \n \n \n\n \n \n \n \n \n \n \n \n \n \n 4.6\n (3 ratings)\n \n \n \n Course Ratings are calculated from individual students’ ratings and a variety of other signals, like age of rating and reliability, to ensure that they reflect course quality fairly and accurately.\n \n \n \n \n \n \n\n\n \n \n \n\n \n \n 30 students enrolled\n \n \n\n\n \n\n ",
"\n \n \n \n \n\n \n New\n \n\n\n \n\n \n \n\n \n \n \n\n \n \n \n \n \n \n \n \n \n \n 0.0\n (0 ratings)\n \n \n \n Course Ratings are calculated from individual students’ ratings and a variety of other signals, like age of rating and reliability, to ensure that they reflect course quality fairly and accurately.\n \n \n \n \n \n \n\n\n \n \n \n\n \n \n 8 students enrolled\n \n \n\n\n \n\n ",
"\n \n \n \n \n\n \n Highest Rated\n \n\n\n \n \n \n\n \n \n \n\n \n \n \n \n \n \n \n \n \n \n 4.6\n (12 ratings)\n \n \n \n Course Ratings are calculated from individual students’ ratings and a variety of other signals, like age of rating and reliability, to ensure that they reflect course quality fairly and accurately.\n \n \n \n \n \n \n\n\n \n \n \n\n \n \n 42 students enrolled\n \n \n\n\n \n\n ",
"\n \n \n \n\n \n \n \n\n \n \n \n \n \n \n \n \n \n \n 4.4\n (6 ratings)\n \n \n \n Course Ratings are calculated from individual students’ ratings and a variety of other signals, like age of rating and reliability, to ensure that they reflect course quality fairly and accurately.\n \n \n \n \n \n \n\n\n \n \n \n\n \n \n 41 students enrolled\n \n \n\n\n \n\n ",
"\n \n \n \n\n \n \n \n\n \n \n \n \n \n \n \n \n \n \n 4.2\n (12 ratings)\n \n \n \n Course Ratings are calculated from individual students’ ratings and a variety of other signals, like age of rating and reliability, to ensure that they reflect course quality fairly and accurately.\n \n \n \n \n \n \n\n\n \n \n \n\n \n \n 115 students enrolled\n \n \n\n\n \n\n ",
"\n \n \n \n\n \n \n \n\n \n \n \n \n \n \n \n \n \n \n 4.8\n (6 ratings)\n \n \n \n Course Ratings are calculated from individual students’ ratings and a variety of other signals, like age of rating and reliability, to ensure that they reflect course quality fairly and accurately.\n \n \n \n \n \n \n\n\n \n \n \n\n \n \n 25 students enrolled\n \n \n\n\n \n\n ",
"\n \n \n \n\n \n \n \n\n \n \n \n \n \n \n \n \n \n \n 4.6\n (19 ratings)\n \n \n \n Course Ratings are calculated from individual students’ ratings and a variety of other signals, like age of rating and reliability, to ensure that they reflect course quality fairly and accurately.\n \n \n \n \n \n \n\n\n \n \n \n\n \n \n 151 students enrolled\n \n \n\n\n \n\n ",
"\n \n \n \n\n \n \n \n\n \n \n \n \n \n \n \n \n \n \n 4.5\n (10 ratings)\n \n \n \n Course Ratings are calculated from individual students’ ratings and a variety of other signals, like age of rating and reliability, to ensure that they reflect course quality fairly and accurately.\n \n \n \n \n \n \n\n\n \n \n \n\n \n \n 385 students enrolled\n \n \n\n\n \n\n ",
"\n \n \n \n\n \n \n \n\n \n \n \n \n \n \n \n \n \n \n 4.8\n (166 ratings)\n \n \n \n Course Ratings are calculated from individual students’ ratings and a variety of other signals, like age of rating and reliability, to ensure that they reflect course quality fairly and accurately.\n \n \n \n \n \n \n\n\n \n \n \n\n \n \n 754 students enrolled\n \n \n\n\n \n\n ",
"\n \n \n \n\n \n \n \n\n \n \n \n \n \n \n \n \n \n \n 3.6\n (34 ratings)\n \n \n \n Course Ratings are calculated from individual students’ ratings and a variety of other signals, like age of rating and reliability, to ensure that they reflect course quality fairly and accurately.\n \n \n \n \n \n \n\n\n \n \n \n\n \n \n 3,396 students enrolled\n \n \n\n\n \n\n "
)
如您所见,有 15 个物体很长而且很脏。但是,它们内部的模式很容易识别。每个对象由 3 个数字组成(以第一个观察为例):
- 评级:从 0 到 5。例如,
4.0 - 评级数。例如
(1 rating) - 已注册学生。例如
9 students enrolled。
我想提取所有这些数值并创建一个包含 3 列的数据框,每列用于每个变量。
我在Stackoverflow这里查了几个问题,主要集中在stringr包的gsub()的使用。但是,我找不到解决问题的关键方法。
更新
这些是我尝试过的代码:
as.numeric(str_extract(rs, "[0-9]+"))
as.numeric(str_extract(rs, "[0-9]+")[[1]])
as.numeric(str_extract(rs, "(?<=\\()[0-9]+(?=\\))"))
as.numeric(sapply(strsplit(rs, " "), "[[", 1))
【问题讨论】:
-
如果您的输入文件有任何规律性,最好告诉我们可能是什么。
-
所以,你检查了一些包。你能展示一些未能提供预期结果的工作吗?它可能只是一个需要调整的正则表达式。我的意思是,您当然做到了,并且不只是希望人们免费为您编写解决方案,对吧?
-
在每个观察中都有三个数字。所以最终的输出应该是一个包含所有这些数字的三列和 15 行的数据框。我用我的一些尝试更新了这个问题。
-
aw,无论谁有两个有效的答案都是 可爱的。他们年轻的时候很可爱。