The American Community Survey distributes downloadable data about United States communities. Download the 2006 microdata survey about housing for the state of Idaho using download.file() from here:
and load the data into R. The code book, describing the variable names is here:
Apply strsplit() to split all the names of the data frame on the characters “wgtp”. What is the value of the 123 element of the resulting list?
Téléchargement du fichier
download.file(fileUrl, destfile = "./data/getdata%2Fdata%2Fss06hid.csv")
PUMSDATA <- read.csv("./data/getdata%2Fdata%2Fss06hid.csv")
Chargement du package dplyr
library(dplyr)
## Warning: package 'dplyr' was built under R version 3.4.4
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
pdata <- tbl_df(PUMSDATA)
Bon certains de vous se demande pourquoi J’ai chargé le package “dplyr”. En fait j’ai appris ce package grâce à un tuto magique sur Coursera. “Swirl package”
Elle affiche très bien les dataframe grâce “tbl_df” et fais pleines d’autres bonnes choses. ## vISUALISONS pdata
pdata
## # A tibble: 6,496 x 188
## RT SERIALNO DIVISION PUMA REGION ST ADJUST WGTP NP TYPE
## <fct> <int> <int> <int> <int> <int> <int> <int> <int> <int>
## 1 H 186 8 700 4 16 1.02e6 89 4 1
## 2 H 306 8 700 4 16 1.02e6 310 1 1
## 3 H 395 8 100 4 16 1.02e6 106 2 1
## 4 H 506 8 700 4 16 1.02e6 240 4 1
## 5 H 835 8 800 4 16 1.02e6 118 4 1
## 6 H 989 8 700 4 16 1.02e6 115 4 1
## 7 H 1861 8 700 4 16 1.02e6 0 1 2
## 8 H 2120 8 200 4 16 1.02e6 35 1 1
## 9 H 2278 8 400 4 16 1.02e6 47 2 1
## 10 H 2428 8 500 4 16 1.02e6 51 2 1
## # ... with 6,486 more rows, and 178 more variables: ACR <int>, AGS <int>,
## # BDS <int>, BLD <int>, BUS <int>, CONP <int>, ELEP <int>, FS <int>,
## # FULP <int>, GASP <int>, HFL <int>, INSP <int>, KIT <int>, MHP <int>,
## # MRGI <int>, MRGP <int>, MRGT <int>, MRGX <int>, PLM <int>, RMS <int>,
## # RNTM <int>, RNTP <int>, SMP <int>, TEL <int>, TEN <int>, VACS <int>,
## # VAL <int>, VEH <int>, WATP <int>, YBL <int>, FES <int>, FINCP <int>,
## # FPARC <int>, GRNTP <int>, GRPIP <int>, HHL <int>, HHT <int>,
## # HINCP <int>, HUGCL <int>, HUPAC <int>, HUPAOC <int>, HUPARC <int>,
## # LNGI <int>, MV <int>, NOC <int>, NPF <int>, NPP <int>, NR <int>,
## # NRC <int>, OCPIP <int>, PARTNER <int>, PSF <int>, R18 <int>,
## # R60 <int>, R65 <int>, RESMODE <int>, SMOCP <int>, SMX <int>,
## # SRNT <int>, SVAL <int>, TAXP <int>, WIF <int>, WKEXREL <int>,
## # WORKSTAT <int>, FACRP <int>, FAGSP <int>, FBDSP <int>, FBLDP <int>,
## # FBUSP <int>, FCONP <int>, FELEP <int>, FFSP <int>, FFULP <int>,
## # FGASP <int>, FHFLP <int>, FINSP <int>, FKITP <int>, FMHP <int>,
## # FMRGIP <int>, FMRGP <int>, FMRGTP <int>, FMRGXP <int>, FMVYP <int>,
## # FPLMP <int>, FRMSP <int>, FRNTMP <int>, FRNTP <int>, FSMP <int>,
## # FSMXHP <int>, FSMXSP <int>, FTAXP <int>, FTELP <int>, FTENP <int>,
## # FVACSP <int>, FVALP <int>, FVEHP <int>, FWATP <int>, FYBLP <int>,
## # wgtp1 <int>, wgtp2 <int>, ...
Revenons
A trop expliquer je n’ai pas pensé que le temps s’écoulait! je dois pouvoir rendre mon devoir Je vais mettre un peu de technique, je vais créer une fonction qui va faire ses deux opérations en même temps Bon je veux aussi que ma fonction agisse uniquement sur les chaine de caractères comportant la chaine “wgtp” et qu’elle ne fasse rien sur d’autre “qualité” de chaine de caractère. J’habite au marécage donc mon style linguistique regardez seulement et avancez… le secret ici c’est la fonction de base “grepl” elle prend deux arguments, pattern = “le caractère que nous cherchons” et x = “le vecteur où nous allons chercher pattern”. Si “grepl” trouve pattern dans x il va renvoyer lui “TRUE” ooh. Et vous connaissez ce qu’il va renvoyer s’il ne trouve pas?..
Bon je crée donc cette fonction. soyons rapide les questions, on le fera après! ### Ma fonction StringSplit
StringSplit <- function(x){if (grepl("wgtp", x)){
strsplit(sub("p", "p_", x), "_")
} else {return(x)}
}
Bon si vous voulez voir comment fonctionne ma fonction regardez donc un exemple
StringSplit("wgtp45")
## [[1]]
## [1] "wgtp" "45"
Vous avez vu n’est ce pas?
Je vais l’appliquer une fois sur toute ma liste (names(pdata)) grâce à un outil que j’ai rencontré dans le module RProgramming de ma formation sur coursera, la fonction “sapply” Elle applique une fonction sur chaque élément d’une liste et retourne une liste. J’espère que vous voyez déjà ce que je veux faire
Je rappelle la question de mon devoir
Apply strsplit() to split all the names of the data frame on the characters “wgtp”. What is the value of the 123 element of the resulting list?
Voilà c’est ça que je suis entrain de faire!
Q1 <- sapply(names(pdata), StringSplit)
Bon je l’ai fait! comme il n’y a pas des écrits de couleurs rouges ce que c’est bon!
Si vous voulez visualisez la liste Q1 et bien la voici
head(Q1, 2)
## $RT
## [1] "RT"
##
## $SERIALNO
## [1] "SERIALNO"
J’ai affiché deux éléments pour ne pas vous fatiguer, cette liste est très longue hein!
Bon maintenant je reponds vraiment à la première question de mon devoir J’étudie comme vous, donc c’est doucement! On m’appelle professeur SAAH, mais je suis encore sur les bancs!
Q1[123]
## $wgtp15
## [1] "wgtp" "15"
Je sais donc ce que je dois cocher comme reponse juste! good!