2016-12-23 30 views
-2

"combo"という変数を作成しようとしています。私は小文字で郡を欲しい.2つの単語の間にスペースがあればスペースを、郡名と州の省略形の間にスペースはない。条件付き末尾削除R

county <- c("Abbeville County", "Aleutians West Census Area", 
      "Cerro Gordo County", "Lonoke County") 
state <- c("West Virginia", "Wisconsin", "Wyoming", "Alabama") 

trialdat <- data.frame(county, state) 
trialdat$state <- sapply(trialdat$state, tolower) 
# deal with trailing spaces 
trim.trailing <- function (x) sub("\\s+$", "", x) 
trialdat$state2 <- as.factor(trim.trailing(as.factor(trialdat$state))) 
trialdat$StateAbbrev <- stateFromLower(trialdat$state2) 
trialdat$county2 <-  as.factor(trim.trailing(as.factor(trialdat$county))) 
# make combo variable 
trialdat = mutate(trialdat, combo=paste(tolower(gsub("County", "",county2)), 
      StateAbbrev, sep="")) 

所望の出力が

     combo 
1     abbevilleWV 
2 aleutians west census areaWI 
3    cerro gordoWY 
4      lonokeAL 

奇妙な事が起こっているとコラムです:

は、これまでのところ、私はこれを持っています。名前にスペースがある郡があれば、私は自分が望むものを手に入れることができます。しかし、他の郡では、郡名の後にスペースが残っています。私は単に郡名の間にそれらが必要なので、すべてのスペースを単に外に出すことはできません。何か案は?ありがとうございました!

注:statefromLower関数は、Chris' codeから少し微調整されています。おそらく問題はこの部分に由来しているかもしれないので、私はそれを含めます。

stateFromLower <- function(x) { 
    # read 52 state codes into local variable [includes DC 
    # (Washington D.C. and PR (Puerto Rico)] 
    st.codes <- data.frame(state1 = as.factor(c("AK", "AL", "AR", 
    "AZ", "CA", "CO", "CT", "DC", "DE", "FL", "GA", "HI", 
    "IA", "ID", "IL", "IN", "KS", "KY", "LA", "MA", "MD", 
    "ME", "MI", "MN", "MO", "MS", "MT", "NC", "ND", "NE", 
    "NH", "NJ", "NM", "NV", "NY", "OH", "OK", "OR", "PA", 
    "PR", "RI", "SC", "SD", "TN", "TX", "UT", "VA", "VT", 
    "WA", "WI", "WV", "WY")), full = as.factor(c("alaska", 
    "alabama", "arkansas", "arizona", "california", "colorado", 
    "connecticut", "district of columbia", "delaware", "florida", 
    "georgia", "hawaii", "iowa", "idaho", "illinois", "indiana", 
    "kansas", "kentucky", "louisiana", "massachusetts", "maryland", 
    "maine", "michigan", "minnesota", "missouri", "mississippi", 
    "montana", "north carolina", "north dakota", "nebraska", 
    "new hampshire", "new jersey", "new mexico", "nevada", 
    "new york", "ohio", "oklahoma", "oregon", "pennsylvania", 
    "puerto rico", "rhode island", "south carolina", "south dakota", 
    "tennessee", "texas", "utah", "virginia", "vermont", 
    "washington", "wisconsin", "west virginia", "wyoming"))) 

    # create an nx1 data.frame of state codes from source column 
    st.x <- data.frame(full = x) 
    # match source codes with codes from 'st.codes' local 
    # variable and use to return the full state name 
    refac.x <- st.codes$state1[match(st.x$full, st.codes$full)] 
    # return the full state names in the same order in which they 
    # appeared in the original source 
    return(refac.x) 
} 

フォーマットに問題がありましたら、これが私の最初の質問です。

+0

ようこそ。 [再現可能な例](https://www.google.com/search?q=how+to+ask+a+great+r+reproducible+example&ie=utf-8&oe=utf-8)を提供できますか?私たちにファイルをダウンロードさせることなく、やろうとしていますか? – C8H10N4O2

+1

また、 'trimws()'を試してみましたか? – C8H10N4O2

+0

再現不可能な例を取り除くことをお勧めします。可能な限り最小限に抑えて、エラーを特定してください。サンプル入力の望ましい出力が可能であることを明確にして、可能なソリューションをテストすることができます。 'stateFromLower'のために投稿したコードは不完全なようです。 – MrFlick

答えて

1

修正済み! mutateコマンドでは、郡の前にスペースを追加する必要がありました。

trialdat = mutate(trialdat, combo=paste(tolower(gsub(" County", "",  county2)), StateAbbrev, sep=""))