合并 First/Last 姓名列并将名字更改为名字首字母

Merging First/Last Name Columns and Changing First Name to Just First Initial

这里的问题有点复杂,所以我会尽量准确。

我正在处理两个不同的数据集:

  1. qb.metrics.df
  2. roster.df

我想将两者合并。通常,我会使用 left_join 但两个数据集之间没有相似的属性。

看一下qb.metrics.df ...

 Rank PlayerPlayerId   PlayerShortName       PlayerAgeExact PassAttemptsPer~ PassingYards CompletionPerce~ PassingTouchdow~
   <dbl>          <dbl> <chr>                    <dbl> <chr>            <chr>        <chr>            <chr>           
 1     1          20889 K. Murray                 23.3 34.6             2375         68.2             17              
 2     2          19801 J. Allen                  24.5 36.4             2873         68.4             21              
 3     3          14536 R. Wilson                 32   37.1             2789         69.8             28              
 4     4          18890 P. Mahomes                25.2 36.6             2687         66.9             25              
 5     5           2593 A. Rodgers                37   34.9             2578         67.8             26              
 6     6           4314 T. Brady                  43.3 38.5             2739         66               23              
 7     7          18857 D. Watson                 25.2 33.4             2539         68.1             18              
 8     8          21681 J. Herbert                22.7 37.6             2333         66.8             19              
 9     9          19781 L. Jackson                23.9 27.4             1762         64               14              
10    10          16497 T. Bridgewater            28   33.1             2544         71.9             13              

这里是 roster.df ...

 season team  position depth_chart_pos~ jersey_number status full_name first_name last_name birth_date height weight
    <dbl> <chr> <chr>    <chr>                    <int> <chr>  <chr>     <chr>      <chr>     <date>     <chr>  <chr> 
 1   2020 ARI   C        C                           52 Active Mason Co~ Mason      Cole      1996-03-28 6-5    292   
 2   2020 ARI   C        C                           53 Active Lamont G~ Lamont     Gaillard  1996-02-08 6-3    305   
 3   2020 ARI   CB       NB                          33 Active Byron Mu~ Byron      Murphy    1998-01-18 5-11   190   
 4   2020 ARI   CB       NA                          20 Active Prince A~ Prince     Amukamara 1989-06-06 6-0    204   
 5   2020 ARI   CB       NA                          39 Active Jace Whi~ Jace       Whittaker 1995-07-16 5-11   185   
 6   2020 ARI   CB       NA                          27 Injur~ Kevin Pe~ Kevin      Peterson  1994-03-22 5-11   185   
 7   2020 ARI   CB       LCB                         25 Active Johnatha~ Johnathan  Joseph    1984-04-16 5-11   185   
 8   2020 ARI   CB       NA                          23 Injur~ Robert A~ Robert     Alford    1988-10-31 5-10   186   
 9   2020 ARI   CB       LCB                         21 Active Patrick ~ Patrick    Peterson  1990-07-11 6-1    203   
10   2020 ARI   CB       RCB                         20 Active Dre Kirk~ Dre        Kirkpatr~ 1989-10-26 6-2    190 

我认为处理这个问题的方法是简单地编辑 roster.df 中的 'full_name' 列以匹配 qb.metrics.df 中的 'PlayerShortName' 列。

这样做需要将名字编辑为第一个首字母,然后添加“.”

不知道该怎么做。

如能提供帮助,我们将不胜感激。同样,我非常乐意采用其他方式来解决这个问题。

一种方法是使用正则表达式方法。

roster.df <- data.frame(full_name = c('Mason Company' ,'Lamont Great', 'Byron Musk'))
roster.df$PlayerShortName <- sub('(.)\w+ (\w+)', '\1. \2', roster.df$full_name)

roster.df
#      full_name PlayerShortName
#1 Mason Company      M. Company
#2  Lamont Great        L. Great
#3    Byron Musk         B. Musk