R 从特定长度中删除字符

R 从特定长度中删除字符,r,numbers,element,string-length,removing-whitespace,R,Numbers,Element,String Length,Removing Whitespace,我的专栏如下: a)1902-2019: -1.05 mm/yr b)1902-1961: -0.79 mm/yr c)1962-2019: -1.43 mm/yr 我想删除要作为流的字符: a)1902-2019 b)1902-1961 c)1962-2019 你介意帮我吗? 提前感谢您可以使用substr df1 <- transform(df1, V3=substr(V1, 1, 9)) df1 # V1 V2

我的专栏如下:

a)1902-2019: -1.05 mm/yr
b)1902-1961: -0.79 mm/yr
c)1962-2019: -1.43 mm/yr
我想删除要作为流的字符:

a)1902-2019
b)1902-1961
c)1962-2019
你介意帮我吗?
提前感谢

您可以使用
substr

df1 <- transform(df1, V3=substr(V1, 1, 9))
df1
#                       V1         V2        V3
# 1 1902-2019: -1.05 mm/yr  1.1088551 1902-2019
# 2 1902-1961: -0.79 mm/yr -0.1593216 1902-1961
# 3 1962-2019: -1.43 mm/yr  0.5811273 1962-2019

df1您可以使用
substr

df1 <- transform(df1, V3=substr(V1, 1, 9))
df1
#                       V1         V2        V3
# 1 1902-2019: -1.05 mm/yr  1.1088551 1902-2019
# 2 1902-1961: -0.79 mm/yr -0.1593216 1902-1961
# 3 1962-2019: -1.43 mm/yr  0.5811273 1962-2019
df1另一个选项:

x <- '1962-2019: -1.43 mm/yr'
gsub(':.*', "", x)
x另一个选项:

x <- '1962-2019: -1.43 mm/yr'
gsub(':.*', "", x)

x另一种方法是将所有内容提取到冒号(


另一种方法是将所有内容提取到冒号(