Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在R中获取列中字符串的最后一个元素?_R_Dplyr_Tidyverse - Fatal编程技术网

在R中获取列中字符串的最后一个元素?

在R中获取列中字符串的最后一个元素?,r,dplyr,tidyverse,R,Dplyr,Tidyverse,我有这样的数据: df <- tribble( ~A, ~B, 'a', '304 49494 3033 23', 'b', '958 49494 5859', 'c', '304 4535 59', ) df您可以删除所有内容,直到最后一个空格获得最后一个元素 df$C <- sub('.*\\s', '', df$B) 谢谢!我非常喜欢dplyr的第二个选项。你知道我能找到的关于dplyr字符串操作的文档吗@Ronak Shahn在stringr程序包中有

我有这样的数据:

df <- tribble(
  ~A, ~B, 
  'a', '304 49494 3033 23', 
  'b', '958 49494 5859',
  'c', '304 4535 59',
)

df您可以删除所有内容,直到最后一个空格获得最后一个元素

df$C <- sub('.*\\s', '', df$B)

谢谢!我非常喜欢dplyr的第二个选项。你知道我能找到的关于dplyr字符串操作的文档吗@Ronak Shahn在
stringr
程序包中有许多用于字符串操作的函数,可以与
dplyr
一起使用。
df$C <- sub('.*\\s', '', df$B)
library(dplyr)
df %>% mutate(C = sub('.*\\s', '', B))

#   A     B                 C    
#  <chr> <chr>             <chr>
#1 a     304 49494 3033 23 23   
#2 b     958 49494 5859    5859 
#3 c     304 4535 59       59