2017-12-24 28 views
2

Ich habe Daten von Paaren, mit Variablen: "Haushaltsnummer", "Haushaltsvorstand", "Bildung", "Einkommen". "Haushaltsnummer" ist die ID-Nummer, die jedem Haushalt eindeutig zugeordnet ist. "Haushaltsvorstand" ist, ob die Person das Oberhaupt des Haushalts ist (1 = Haushaltsvorstand, 2 = Ehepartner des Haushaltsvorstandes), "Bildung" und "Einkommen" Bildungsstand bzw. Einkommen des Einzelnen. Zum Beispiel sehen die Daten wie folgt aus.Wie erstelle ich eine Ehepartnervariable?

'household_number' 'head_of_household' 'education' 'income' 
     1      1    high  1000 
     1      2    low  100 
     3      1    medium  500 
     3      2    high  800 
     4      2    high  800 
     4      1    high  800 
     9      1    low  150 
     9      2    low  200 

Ich möchte die Variable des Ehepartners für jedes Individuum erstellen. So dass die Daten wie folgt aussehen. Wenn "Ehegatte edu" das Bildungsniveau des Ehegatten ist und "Ehegatte inc" das Einkommen des Ehegatten.

'household_number' 'head_of_household' 'education' 'income' 'spouse_edu' 'spouse_inc' 
     1      1    high  1000  low   100 
     1      2    low  100  high  1000 
     3      1    medium  500  high  800 
     3      2    high  800  medium  500 
     4      2    high  800  high  800 
     4      1    high  800  high  800 
     9      1    low  150  low   200 
     9      2    low  200  low   150 

Ich habe sehr große Datenmenge, also suche ich nach einer einfachen Möglichkeit, dies zu tun. Gibt es eine elegante Möglichkeit, dies zu tun?

Unten ist reproduzierbare Beispielsyntax.

household_number <- c(1,1,3,3,4,4,9,9) 
head_of_household <- c(1,2,1,2,2,1,1,2) 
education <- c("high", "low", "medium", "high", "high", "high", "low", "low") 
income <- c(1000, 100, 500, 800, 800, 800, 150, 200) 

data <- data.frame(household_number, head_of_household, education, income) 
+0

'data.table' wird Ihnen helfen. – MKR

Antwort

6

können Sie base::rev und dplyr hier verwenden.

library(dplyr) 
data %>% 
group_by(household_number) %>% 
mutate(spouse_income = rev(income), 
     spouse_education = rev(education)) %>% 
ungroup() 

# A tibble: 8 x 6 
    household_number head_of_household education income spouse_income spouse_education 
      <dbl>    <dbl> <fctr> <dbl>   <dbl>   <fctr> 
1    1     1  high 1000   100    low 
2    1     2  low 100   1000    high 
3    3     1 medium 500   800    high 
4    3     2  high 800   500   medium 
5    4     2  high 800   800    high 
6    4     1  high 800   800    high 
7    9     1  low 150   200    low 
8    9     2  low 200   150    low 

Eine Lösung data.table verwenden.

library(data.table) 
data_DT <- as.data.table(data) 
data_DT[ , c("spouse_income", "spouse_education") := list(rev(income), rev(education)), by = household_number] 
data_DT 

    household_number head_of_household education income spouse_income spouse_education 
1:    1     1  high 1000   100    low 
2:    1     2  low 100   1000    high 
3:    3     1 medium 500   800    high 
4:    3     2  high 800   500   medium 
5:    4     2  high 800   800    high 
6:    4     1  high 800   800    high 
7:    9     1  low 150   200    low 
8:    9     2  low 200   150    low 
+0

wirklich schöne Lösung, um die Zeilen zu "wechseln", was das OP wollte. 'group_by()%>% mutate()' ist schlau – InfiniteFlashChess

1

Der andere Weg, dies mit shift in data.table zu lösen. Es wird jedoch ein zweistufiger Prozess sein.

Erste Gruppe von auf household_number und Ehepartner Einzelheiten 1. Satz füllen mit shift mit lag

data[,':='(
     spouse_edu = shift(education), 
     spouse_inc = shift(income)), 
     by = household_number] 
> data 
    household_number head_of_household education income spouse_edu spouse_inc 
1:    1     1  high 1000   NA   NA 
2:    1     2  low 100  high  1000 
3:    3     1 medium 500   NA   NA 
4:    3     2  high 800  medium  500 
5:    4     2  high 800   NA   NA 
6:    4     1  high 800  high  800 
7:    9     1  low 150   NA   NA 
8:    9     2  low 200  low  150 

Nun füllen Ehepartner Details für andere Satz mit lead Art von shift. Stellen Sie sicher, dass wir keine Angaben zum Ehegatten ersetzen, die bereits ausgefüllt oder aktualisiert wurden.

data[,':='(
     spouse_edu = ifelse(is.na(spouse_edu), shift(education, type="lead"), spouse_edu) , 
     spouse_inc = ifelse(is.na(spouse_inc), shift(income, type="lead"), spouse_inc)), 
     by = household_number] 
> data 
    household_number head_of_household education income spouse_edu spouse_inc 
1:    1     1  high 1000  low  100 
2:    1     2  low 100  high  1000 
3:    3     1 medium 500  high  800 
4:    3     2  high 800  medium  500 
5:    4     2  high 800  high  800 
6:    4     1  high 800  high  800 
7:    9     1  low 150  low  200 
8:    9     2  low 200  low  150