2012-10-24 5 views
1

J'ai ces données:comment insérer plusieurs lignes

hhid perid actNo thisAct from to tripTime 
8019450  1  1 home 180 1051  NA 
8019450  1  2 school 1075 1245  24 
8019450  1  3 socrec 1255 1260  10 
8019450  1  4 home 1280 1619  20 

Maintenant, je veux insérer trois lignes dans lesquelles

thisAct=travel 
from=(from-tripTime-1) 
to=(from-1) 

Ensuite, les données attendues ressemblent à ceci:

hhid perid actNo thisAct from to tripTime 
8019450  1  1 home 180 1051  NA 
*8019450 1  2 travel 1052 1074   
8019450  1  3 school 1075 1245  24 
*8019450 1  4 travel 1246 1254   
8019450  1  5 socrec 1255 1260  10 
*8019450 1  6 travel 1261 1279   
8019450  1  7 home 1280 1619  20 

Pourriez-vous s'il vous plaît comment insérer ces lignes avec des astérisques?

Merci.

+0

Existe-t-il un schéma général de la façon dont vous devez insérer vos données, ou avez-vous littéralement besoin d'insérer ces 3 lignes parmi ces 4 lignes existantes? – DuckMaestro

+0

Oui, il y a un schéma général. Merci. – POTENZA

Répondre

3

Démarrer en recréant vos données:

dat <- read.table(text=" 
        hhid perid actNo thisAct from to tripTime 
c  1  1 home 180 1051  NA 
8019450  1  2 school 1075 1245  24 
8019450  1  3 socrec 1255 1260  10 
8019450  1  4 home 1280 1619  20 
        ", header=TRUE) 

Calculons maintenant les temps de déplacement et de le mettre dans une trame de données avec la même forme que vos données

travel <- data.frame(
    hhid = 8019450, 
    perid = 1, 
    actNo = NA, 
    thisAct = "travel", 
    from = head(dat$to + 1, -1), 
    to = tail(dat$from - 1, -1), 
    tripTime = NA 
) 

puis rbind et trier:

x <- rbind(dat, travel) 
x <- x[order(x$from), ] 
x$perid <- seq_along(x$perid) 
x 

    hhid perid actNo thisAct from to tripTime 
1  c  1  1 home 180 1051  NA 
5 8019450  2 NA travel 1052 1074  NA 
2 8019450  3  2 school 1075 1245  24 
6 8019450  4 NA travel 1246 1254  NA 
3 8019450  5  3 socrec 1255 1260  10 
7 8019450  6 NA travel 1261 1279  NA 
4 8019450  7  4 home 1280 1619  20 
1

Vos données:

dat <- read.table(text="hhid perid actNo thisAct from to tripTime 
8019450 1 1 home 180 1051 NA 
8019450 1 2 school 1075 1245 24 
8019450 1 3 socrec 1255 1260 10 
8019450 1 4 home 1280 1619 20", header = TRUE, stringsAsFactors = FALSE) 

C'est un moyen d'obtenir ce que vous voulez:

dat2<- dat[c(1, rep(2:nrow(dat), each = 2)), ] 
dat2$actNo <- 1:nrow(dat2) 
dat2[c(FALSE, TRUE), "thisAct"] <- "travel" 
dat2[c(FALSE, TRUE), "to"] <- dat2[c(FALSE, TRUE), "from"] - 1 
dat2[c(FALSE, TRUE), "from"] <- (dat2[c(FALSE, TRUE), "from"] - 
           dat2[c(FALSE, TRUE), "tripTime"]) + 1  
dat2[c(FALSE, TRUE), "tripTime"] <- NA 

Puisque la valeur dans la colonne tripTime n'a pas été précisée, j'ai choisi NA pour les nouvelles colonnes.

La sortie:

#  hhid perid actNo thisAct from to tripTime 
# 1 8019450  1  1 home 180 1051  NA 
# 2 8019450  1  2 travel 1052 1074  NA 
# 2.1 8019450  1  3 school 1075 1245  24 
# 3 8019450  1  4 travel 1246 1254  NA 
# 3.1 8019450  1  5 socrec 1255 1260  10 
# 4 8019450  1  6 travel 1261 1279  NA 
# 4.1 8019450  1  7 home 1280 1619  20 
Questions connexes