我有一个这样的数据框:
2014/15 All codes All admissions
2014/15 C33-C34 All diseases which can be caused by smoking
2014/15 C00-D48 All cancers
2014/15 J00-J99 All respiratory diseases
2014/15 I00-I99 All circulatory diseases
我想这样做一年:
2014 All codes All admissions
2014 C33-C34 All diseases which can be caused by smoking
2014 C00-D48 All cancers
2014 J00-J99 All respiratory diseases
2014 I00-I99 All circulatory diseases
我该怎么做?
P.S。请记住,我有更多的年份,如2015 / 16,2009 / 10 ...... 所以基本上我想删除" /"之后的所有内容。 我也使用ggplot2和tidyverse。
提前谢谢
答案 0 :(得分:1)
我们可以使用substr
,假设前4个字符始终为year
admissions$Year <- substr(admissions$Year, 1, 4)
答案 1 :(得分:0)
或使用sub:
x<-"2014/15"
sub("/.*","",x)
[1] "2014"
在你的情况下
gsub ("/.*", "", admissions$Year)