2017-08-27 14 views
-2

數據集是從ISLR包「工資」中刪除數字或文本元素如何從所有列在這個問題中使用

library(ISLR) 

    head(Wage) 

    year age   maritl  race  education    region  jobclass   health 
1 2006 18 1. Never Married 1. White 1. < HS Grad 2. Middle Atlantic 1. Industrial  1. <=Good 
2 2004 24 1. Never Married 1. White 4. College Grad 2. Middle Atlantic 2. Information 2. >=Very Good 
3 2003 45  2. Married 1. White 3. Some College 2. Middle Atlantic 1. Industrial  1. <=Good 
    health_ins logwage  wage 
1  2. No 4.318063 75.04315 
2  2. No 4.255273 70.47602 
3  1. Yes 4.875061 130.98218 

到第九列第三列包含不想要的字符(第一元件),例如1或2個。

如何刪除所有不必要的字符和數字爲所有提到的列

+1

宣嗨,看看這裏https://stackoverflow.com/help/how-to-ask這裏:https://stackoverflow.com/questions/5963269/如何使一個偉大的可重現的例子和修改你的問題。也可以在http://tidyverse.org/看看你的直接問題 – Jan

回答

1

變異所有 「[1-9]。」

library(dplyr) 
temp <- Wage 
ans <- temp %>% 
     mutate_at(3:9, funs(sub("\\d. ", "", .))) 

輸出

head(ans) 

    year age  maritl race education   region jobclass  health 
1 2006 18 Never Married White < HS Grad Middle Atlantic Industrial  <=Good 
2 2004 24 Never Married White College Grad Middle Atlantic Information >=Very Good 
3 2003 45  Married White Some College Middle Atlantic Industrial  <=Good 
4 2003 43  Married Asian College Grad Middle Atlantic Information >=Very Good 
5 2005 50  Divorced White  HS Grad Middle Atlantic Information  <=Good 
6 2008 54  Married White College Grad Middle Atlantic Information >=Very Good 
    health_ins logwage  wage 
1   No 4.318063 75.04315 
2   No 4.255273 70.47602 
3  Yes 4.875061 130.98218 
4  Yes 5.041393 154.68529 
5  Yes 4.318063 75.04315 
6  Yes 4.845098 127.11574 
+0

謝謝@Chi Pak。但任何其他方式刪除4.,3,或許多其他方式wuthout寫太多「mutate_at(3:9,funs(sub(」1「,」「,。)))」 – Tuyen

+0

@Tuyen你也可以做它像這樣:'temp%>%mutate_at(3:9,funs(sub(「[12]。」,「」,。)))' – Jaap

+0

@Tuyen試試我編輯的答案 – CPak

相關問題