在 R 中显示包含一系列数字(0-9 之间)的行数
Display number of rows containing a range of numbers (between 0-9) in R
我正在尝试使用 R 计算我的数据集(称为数据)中包含一系列数字(例如 0 到 9 之间)的行数。我没有创建数据框,我的数据集是直接导入的从 csv 文件到 R.
数据集示例(输入)
MESSAGE
I have to wait 3 days
Feel quite tired
No way is 7pm already
It is too late now
This is beautiful
所以输出将是 2 行(第 1 行和第 2 行)
我尝试了以下代码,但它为我提供了错误的帖子输出数 (3) - 所以我知道我肯定做错了什么。
data = read.csv (xxxxxx)
#count number of rows that contain numbers between 0 and 9
numbers= filter(data, !grepl("[0-9]",MESSAGE))
length(numbers)
提前致谢。
如果至少有一个数字,也许你可以试试下面的代码
> length(grep("\d", MESSAGE, value = TRUE))
[1] 2
如果你想找出有单个数字的行,你可以试试
> length(grep("\b\d(?![0-9])", MESSAGE, value = TRUE, perl = TRUE))
[1] 2
数据
MESSAGE <- c(
"I have to wait 3 days",
"Feel quite tired",
"No way is 7pm already",
"It is too late now",
"This is beautiful"
)
filter
函数 returns 数据帧返回并计算 length
数据帧 returns 列数而不是行数。您还通过在前面引入 !
来对 select 没有数字的行使用正则表达式。
您可以使用 sum
。 + grepl
:
result <- sum(grepl('[0-9]', data$MESSAGE))
我正在尝试使用 R 计算我的数据集(称为数据)中包含一系列数字(例如 0 到 9 之间)的行数。我没有创建数据框,我的数据集是直接导入的从 csv 文件到 R.
数据集示例(输入)
MESSAGE |
---|
I have to wait 3 days |
Feel quite tired |
No way is 7pm already |
It is too late now |
This is beautiful |
所以输出将是 2 行(第 1 行和第 2 行)
我尝试了以下代码,但它为我提供了错误的帖子输出数 (3) - 所以我知道我肯定做错了什么。
data = read.csv (xxxxxx)
#count number of rows that contain numbers between 0 and 9
numbers= filter(data, !grepl("[0-9]",MESSAGE))
length(numbers)
提前致谢。
如果至少有一个数字,也许你可以试试下面的代码
> length(grep("\d", MESSAGE, value = TRUE))
[1] 2
如果你想找出有单个数字的行,你可以试试
> length(grep("\b\d(?![0-9])", MESSAGE, value = TRUE, perl = TRUE))
[1] 2
数据
MESSAGE <- c(
"I have to wait 3 days",
"Feel quite tired",
"No way is 7pm already",
"It is too late now",
"This is beautiful"
)
filter
函数 returns 数据帧返回并计算 length
数据帧 returns 列数而不是行数。您还通过在前面引入 !
来对 select 没有数字的行使用正则表达式。
您可以使用 sum
。 + grepl
:
result <- sum(grepl('[0-9]', data$MESSAGE))