R - remove anything after comma from column

后端 未结 5 1684
死守一世寂寞
死守一世寂寞 2020-12-02 00:18

I\'d like to strip this column so that it just shows last name - if there is a comma I\'d like to remove the comma and anything after it. I have data column that is a mix of

5条回答
  •  旧巷少年郎
    2020-12-02 01:05

    Also try strsplit:

    string <- c("Sample, A", "Tester", "Wifred, Nancy", "Day, Bobby Jean", "Morris")
    
    sapply(strsplit(string, ","), "[", 1)
    #[1] "Sample" "Tester" "Wifred" "Day"    "Morris"
    

提交回复
热议问题