简体   繁体   中英

How to split a list of strings into odd and even elements in R

Sorry, I'm failing at even the most elementary part of this task. I have a 50-long list of strings, and I initialize an empty vector 25 long, but even manually assigning the 1st element of the one to the 1st element of the other fails miserably:

> p$key
 [1] CA TX NY FL IL PA OH MI GA NC NJ VA MA WA IN AZ TN MO MD WI MN CO AL SC LA KY OR
[28] OK CT IA MS AR KS UT NV NM WV NE ID ME NH HI RI MT DE SD AK ND VT WY
50 Levels: AK AL AR AZ CA CO CT DE FL GA HI IA ID IL IN KS KY LA MA MD ME MI ... WY
> st <- character(25)
> st[1] <- p$key[1]
> st[1]
[1] "5"
> p$key[1]
[1] CA
50 Levels: AK AL AR AZ CA CO CT DE FL GA HI IA ID IL IN KS KY LA MA MD ME MI ... WY
>

Sorry I'm so dense, can you please point out what I'm ignorant of here? Thanks - EdB

Your p$key is a factor , so do st[1] = as.character(p$key[1]) instead.

Assuming you want character lists, this will give you your splits:

odds = as.character(p$key[seq(1,50,2)])
evens = as.character(p$key[seq(2,50,2)])

Try this:

> key <- factor(state.abb)
> key
 [1] AL AK AZ AR CA CO CT DE FL GA HI ID IL IN IA KS KY LA ME MD MA MI MN MS MO MT
[27] NE NV NH NJ NM NY NC ND OH OK OR PA RI SC SD TN TX UT VT VA WA WV WI WY
50 Levels: AK AL AR AZ CA CO CT DE FL GA HI IA ID IL IN KS KY LA MA MD ME ... WY

If you want every alternate value and want to retain all the factor levels, use:

> split(key, 1:length(key) %% 2 == 0)
$`FALSE`
 [1] AL AZ CA CT FL HI IL IA KY ME MA MN MO NE NH NM NC OH OR RI SD TX VT WA WI
50 Levels: AK AL AR AZ CA CO CT DE FL GA HI IA ID IL IN KS KY LA MA MD ME ... WY

$`TRUE`
 [1] AK AR CO DE GA ID IN KS LA MD MI MS MT NV NJ NY ND OK PA SC TN UT VA WV WY
50 Levels: AK AL AR AZ CA CO CT DE FL GA HI IA ID IL IN KS KY LA MA MD ME ... WY

If you prefer your output as character values, use:

> split(as.character(key), 1:length(key) %% 2 == 0)
$`FALSE`
 [1] "AL" "AZ" "CA" "CT" "FL" "HI" "IL" "IA" "KY" "ME" "MA" "MN" "MO" "NE" "NH"
[16] "NM" "NC" "OH" "OR" "RI" "SD" "TX" "VT" "WA" "WI"

$`TRUE`
 [1] "AK" "AR" "CO" "DE" "GA" "ID" "IN" "KS" "LA" "MD" "MI" "MS" "MT" "NV" "NJ"
[16] "NY" "ND" "OK" "PA" "SC" "TN" "UT" "VA" "WV" "WY"

Update

If you prefer separate vectors instead of a list, try:

stOdds <- key[1:length(key) %% 2 == 1]
stEvens <- key[1:length(key) %% 2 == 0]

This will return every other element. The first returns every other element staring with the first and the second returns a vector starting with the second.

>  key <- factor(state.abb)
> key[c(TRUE, FALSE)]
 [1] AL AZ CA CT FL HI IL IA KY ME MA MN MO NE NH NM NC OH OR RI SD TX VT WA WI
50 Levels: AK AL AR AZ CA CO CT 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 ... WY
> key[c(FALSE,TRUE)]
 [1] AK AR CO DE GA ID IN KS LA MD MI MS MT NV NJ NY ND OK PA SC TN UT VA WV WY
50 Levels: AK AL AR AZ CA CO CT 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 ... WY

As others have already pointed out using as.character() will convert to a non-factor object that doesn't have all those distracting levels.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM