在R中,如何从字符中提取分和秒
我正在尝试将R中的一列拆分为分和秒。问题是,该列只是数字列:例如,它将有“752”、“843”、“823”、“956”(最高可达2000左右)等。。。代表7分52秒,8分43秒,8分23秒等等。。。我想把它分成两列。一列表示分钟数,一列表示秒数。然后,我将使用这些列创建第三个“totalSeconds”,它在7分52秒的时间内有“472” 我一直在到处搜索,检查正则表达式等等。。我只是想不通。这里的另一个类似问题将我引向函数“substr”,但我一直坚持这一点,因为每个变量中的字符数并不总是相同的,即752对1145。有什么帮助吗?或者至少有人能给我指出正确的方向?在本例中:在R中,如何从字符中提取分和秒,r,time,R,Time,我正在尝试将R中的一列拆分为分和秒。问题是,该列只是数字列:例如,它将有“752”、“843”、“823”、“956”(最高可达2000左右)等。。。代表7分52秒,8分43秒,8分23秒等等。。。我想把它分成两列。一列表示分钟数,一列表示秒数。然后,我将使用这些列创建第三个“totalSeconds”,它在7分52秒的时间内有“472” 我一直在到处搜索,检查正则表达式等等。。我只是想不通。这里的另一个类似问题将我引向函数“substr”,但我一直坚持这一点,因为每个变量中的字符数并不总是相同
df=data.frame(D=round(1000*runif(100)))
D是包含字符串的列。如果您这样做:
df$MIN=ifelse(nchar(df$D)>=3,substr(df$D, 0,1),0) #if there are only seconds
这将返回会议记录
及
将返回秒数
这就是您想要的吗?在本例中:
df=data.frame(D=round(1000*runif(100)))
D是包含字符串的列。如果您这样做:
df$MIN=ifelse(nchar(df$D)>=3,substr(df$D, 0,1),0) #if there are only seconds
这将返回会议记录
及
将返回秒数
这就是您想要的吗?在本例中:
df=data.frame(D=round(1000*runif(100)))
D是包含字符串的列。如果您这样做:
df$MIN=ifelse(nchar(df$D)>=3,substr(df$D, 0,1),0) #if there are only seconds
这将返回会议记录
及
将返回秒数
这就是您想要的吗?在本例中:
df=data.frame(D=round(1000*runif(100)))
D是包含字符串的列。如果您这样做:
df$MIN=ifelse(nchar(df$D)>=3,substr(df$D, 0,1),0) #if there are only seconds
这将返回会议记录
及
将返回秒数
这就是您想要的吗?如果字符的最后两个元素表示秒,剩余的前一个/两个元素表示分钟,请尝试以下操作:
res <- data.frame(v = v, minutes = substr(v, 1, nchar(v)-2), seconds = substr(v, nchar(v)-1, nchar(v)))
> res
v minutes seconds
1 752 7 52
2 843 8 43
3 823 8 23
4 956 9 56
res
v分秒
1 752 7 52
2 843 8 43
3 823 8 23
4 956 9 56
要计算总秒数,请执行以下操作:
res <- as.data.frame(apply(res, 2, function(x) as.double(as.character(x))))
res$tot.sec <- res$minutes*60 + res$seconds
> res
v minutes seconds tot.sec
1 752 7 52 472
2 843 8 43 523
3 823 8 23 503
4 956 9 56 596
res如果字符的最后两个元素表示秒,剩余的前一个/两个元素表示分钟,请尝试以下操作:
res <- data.frame(v = v, minutes = substr(v, 1, nchar(v)-2), seconds = substr(v, nchar(v)-1, nchar(v)))
> res
v minutes seconds
1 752 7 52
2 843 8 43
3 823 8 23
4 956 9 56
res
v分秒
1 752 7 52
2 843 8 43
3 823 8 23
4 956 9 56
要计算总秒数,请执行以下操作:
res <- as.data.frame(apply(res, 2, function(x) as.double(as.character(x))))
res$tot.sec <- res$minutes*60 + res$seconds
> res
v minutes seconds tot.sec
1 752 7 52 472
2 843 8 43 523
3 823 8 23 503
4 956 9 56 596
res如果字符的最后两个元素表示秒,剩余的前一个/两个元素表示分钟,请尝试以下操作:
res <- data.frame(v = v, minutes = substr(v, 1, nchar(v)-2), seconds = substr(v, nchar(v)-1, nchar(v)))
> res
v minutes seconds
1 752 7 52
2 843 8 43
3 823 8 23
4 956 9 56
res
v分秒
1 752 7 52
2 843 8 43
3 823 8 23
4 956 9 56
要计算总秒数,请执行以下操作:
res <- as.data.frame(apply(res, 2, function(x) as.double(as.character(x))))
res$tot.sec <- res$minutes*60 + res$seconds
> res
v minutes seconds tot.sec
1 752 7 52 472
2 843 8 43 523
3 823 8 23 503
4 956 9 56 596
res如果字符的最后两个元素表示秒,剩余的前一个/两个元素表示分钟,请尝试以下操作:
res <- data.frame(v = v, minutes = substr(v, 1, nchar(v)-2), seconds = substr(v, nchar(v)-1, nchar(v)))
> res
v minutes seconds
1 752 7 52
2 843 8 43
3 823 8 23
4 956 9 56
res
v分秒
1 752 7 52
2 843 8 43
3 823 8 23
4 956 9 56
要计算总秒数,请执行以下操作:
res <- as.data.frame(apply(res, 2, function(x) as.double(as.character(x))))
res$tot.sec <- res$minutes*60 + res$seconds
> res
v minutes seconds tot.sec
1 752 7 52 472
2 843 8 43 523
3 823 8 23 503
4 956 9 56 596
<代码> RES你可以从“Tiyr”中考虑<代码>分开<代码>。在这里,我将它与“dplyr”中的
mutate
结合使用,以获得所需的输出
请注意,sep
允许您根据sep
值是正值还是负值,从字符串的左侧或右侧指定。这将使我们能够适当地处理像“1000”这样的案件
library(dplyr)
library(tidyr)
df %>%
separate(secs, into = c("min", "sec"), sep = -3) %>%
mutate(tot = as.numeric(min)*60 + as.numeric(sec))
# min sec tot
# 1 7 52 472
# 2 8 43 523
# 3 8 23 503
# 4 9 56 596
# 5 10 00 600
样本数据:
<代码> df> p>您可以从“Tidir”中考虑<代码>分隔<代码>。在这里,我将它与“dplyr”中的mutate
结合使用,以获得所需的输出
请注意,sep
允许您根据sep
值是正值还是负值,从字符串的左侧或右侧指定。这将使我们能够适当地处理像“1000”这样的案件
library(dplyr)
library(tidyr)
df %>%
separate(secs, into = c("min", "sec"), sep = -3) %>%
mutate(tot = as.numeric(min)*60 + as.numeric(sec))
# min sec tot
# 1 7 52 472
# 2 8 43 523
# 3 8 23 503
# 4 9 56 596
# 5 10 00 600
样本数据:
<代码> df> p>您可以从“Tidir”中考虑<代码>分隔<代码>。在这里,我将它与“dplyr”中的mutate
结合使用,以获得所需的输出
请注意,sep
允许您根据sep
值是正值还是负值,从字符串的左侧或右侧指定。这将使我们能够适当地处理像“1000”这样的案件
library(dplyr)
library(tidyr)
df %>%
separate(secs, into = c("min", "sec"), sep = -3) %>%
mutate(tot = as.numeric(min)*60 + as.numeric(sec))
# min sec tot
# 1 7 52 472
# 2 8 43 523
# 3 8 23 503
# 4 9 56 596
# 5 10 00 600
样本数据:
<代码> df> p>您可以从“Tidir”中考虑<代码>分隔<代码>。在这里,我将它与“dplyr”中的mutate
结合使用,以获得所需的输出
请注意,sep
允许您根据sep
值是正值还是负值,从字符串的左侧或右侧指定。这将使我们能够适当地处理像“1000”这样的案件
library(dplyr)
library(tidyr)
df %>%
separate(secs, into = c("min", "sec"), sep = -3) %>%
mutate(tot = as.numeric(min)*60 + as.numeric(sec))
# min sec tot
# 1 7 52 472
# 2 8 43 523
# 3 8 23 503
# 4 9 56 596
# 5 10 00 600
样本数据:
df 2000代表什么?@RStudent我想那将是20分钟2000代表什么?@RStudent我想那将是20分钟2000代表什么?@RStudent我想那将是20分钟2000代表什么?@RStudent我想那将是20分钟这差不多了,它将像815一样的东西正确地分开,但是像1000这样的东西会被弄乱,变成1和00。这差不多,它会正确地分割815这样的东西,但是像1000这样的东西会被弄乱,变成1和00。这差不多,它会正确地分割815这样的东西,但是像1000这样的东西会被弄乱,变成1和00。这差不多了,它可以正确地拆分815这样的值,但1000这样的值会被弄乱,变成1和00。