Fill row values to the right of some value











up vote
1
down vote

favorite












Given the following data frame, I need to be able to fill out values in each row to the right until the next value is encountered in which case I need to fill that value out etc. until I reach the end of the row.



# load data
id <- LETTERS[1:7]
X2000 <- c(NA,NA,NA,NA,100,NA,NA)
X2001 <- c(NA,200,80,NA,205,50,NA)
X2002 <- c(NA,300,NA,300,NA,NA,NA)
X2003 <- c(400,NA,70,NA,NA,NA,600)
X2004 <- c(NA,500,NA,NA,NA,NA,NA)
dat <- data.frame(id,X2000,X2001,X2002,X2003,X2004)

id X2000 X2001 X2002 X2003 X2004
A NA NA NA 400 NA
B NA 200 300 NA 500
C NA 80 NA 70 NA
D NA NA 300 NA NA
E 100 205 NA NA NA
F NA 50 NA NA NA
G NA NA NA 600 NA


The resulting dataframe should look like this:



id X2000 X2001 X2002 X2003 X2004
A NA NA NA 400 400
B NA 200 300 300 500
C NA 80 80 70 70
D NA NA 300 300 300
E 100 205 205 205 205
F NA 50 50 50 50
G NA NA NA 600 600


Any clever way of doing this? Thanks.










share|improve this question






















  • Related posts. cbind(dat[1], t(zoo::na.locf(t(dat[-1]))))
    – Henrik
    2 days ago

















up vote
1
down vote

favorite












Given the following data frame, I need to be able to fill out values in each row to the right until the next value is encountered in which case I need to fill that value out etc. until I reach the end of the row.



# load data
id <- LETTERS[1:7]
X2000 <- c(NA,NA,NA,NA,100,NA,NA)
X2001 <- c(NA,200,80,NA,205,50,NA)
X2002 <- c(NA,300,NA,300,NA,NA,NA)
X2003 <- c(400,NA,70,NA,NA,NA,600)
X2004 <- c(NA,500,NA,NA,NA,NA,NA)
dat <- data.frame(id,X2000,X2001,X2002,X2003,X2004)

id X2000 X2001 X2002 X2003 X2004
A NA NA NA 400 NA
B NA 200 300 NA 500
C NA 80 NA 70 NA
D NA NA 300 NA NA
E 100 205 NA NA NA
F NA 50 NA NA NA
G NA NA NA 600 NA


The resulting dataframe should look like this:



id X2000 X2001 X2002 X2003 X2004
A NA NA NA 400 400
B NA 200 300 300 500
C NA 80 80 70 70
D NA NA 300 300 300
E 100 205 205 205 205
F NA 50 50 50 50
G NA NA NA 600 600


Any clever way of doing this? Thanks.










share|improve this question






















  • Related posts. cbind(dat[1], t(zoo::na.locf(t(dat[-1]))))
    – Henrik
    2 days ago















up vote
1
down vote

favorite









up vote
1
down vote

favorite











Given the following data frame, I need to be able to fill out values in each row to the right until the next value is encountered in which case I need to fill that value out etc. until I reach the end of the row.



# load data
id <- LETTERS[1:7]
X2000 <- c(NA,NA,NA,NA,100,NA,NA)
X2001 <- c(NA,200,80,NA,205,50,NA)
X2002 <- c(NA,300,NA,300,NA,NA,NA)
X2003 <- c(400,NA,70,NA,NA,NA,600)
X2004 <- c(NA,500,NA,NA,NA,NA,NA)
dat <- data.frame(id,X2000,X2001,X2002,X2003,X2004)

id X2000 X2001 X2002 X2003 X2004
A NA NA NA 400 NA
B NA 200 300 NA 500
C NA 80 NA 70 NA
D NA NA 300 NA NA
E 100 205 NA NA NA
F NA 50 NA NA NA
G NA NA NA 600 NA


The resulting dataframe should look like this:



id X2000 X2001 X2002 X2003 X2004
A NA NA NA 400 400
B NA 200 300 300 500
C NA 80 80 70 70
D NA NA 300 300 300
E 100 205 205 205 205
F NA 50 50 50 50
G NA NA NA 600 600


Any clever way of doing this? Thanks.










share|improve this question













Given the following data frame, I need to be able to fill out values in each row to the right until the next value is encountered in which case I need to fill that value out etc. until I reach the end of the row.



# load data
id <- LETTERS[1:7]
X2000 <- c(NA,NA,NA,NA,100,NA,NA)
X2001 <- c(NA,200,80,NA,205,50,NA)
X2002 <- c(NA,300,NA,300,NA,NA,NA)
X2003 <- c(400,NA,70,NA,NA,NA,600)
X2004 <- c(NA,500,NA,NA,NA,NA,NA)
dat <- data.frame(id,X2000,X2001,X2002,X2003,X2004)

id X2000 X2001 X2002 X2003 X2004
A NA NA NA 400 NA
B NA 200 300 NA 500
C NA 80 NA 70 NA
D NA NA 300 NA NA
E 100 205 NA NA NA
F NA 50 NA NA NA
G NA NA NA 600 NA


The resulting dataframe should look like this:



id X2000 X2001 X2002 X2003 X2004
A NA NA NA 400 400
B NA 200 300 300 500
C NA 80 80 70 70
D NA NA 300 300 300
E 100 205 205 205 205
F NA 50 50 50 50
G NA NA NA 600 600


Any clever way of doing this? Thanks.







r






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked 2 days ago









user1658170

1711617




1711617












  • Related posts. cbind(dat[1], t(zoo::na.locf(t(dat[-1]))))
    – Henrik
    2 days ago




















  • Related posts. cbind(dat[1], t(zoo::na.locf(t(dat[-1]))))
    – Henrik
    2 days ago


















Related posts. cbind(dat[1], t(zoo::na.locf(t(dat[-1]))))
– Henrik
2 days ago






Related posts. cbind(dat[1], t(zoo::na.locf(t(dat[-1]))))
– Henrik
2 days ago














2 Answers
2






active

oldest

votes

















up vote
1
down vote



accepted










Here's one way with dplyr and tidyr -



dat %>%
gather(year, value, -id) %>%
group_by(id) %>%
arrange(id, year) %>%
fill(value, .direction = "down") %>%
ungroup() %>%
spread(year, value)

# A tibble: 7 x 6
id X2000 X2001 X2002 X2003 X2004
<fct> <dbl> <dbl> <dbl> <dbl> <dbl>
1 A NA NA NA 400 400
2 B NA 200 300 300 500
3 C NA 80.0 80.0 70.0 70.0
4 D NA NA 300 300 300
5 E 100 205 205 205 205
6 F NA 50.0 50.0 50.0 50.0
7 G NA NA NA 600 600





share|improve this answer





















  • This was perfect
    – user1658170
    11 hours ago


















up vote
1
down vote













We could apply with na.locf



library(zoo)
dat[-1] <- t(apply(dat[-1], 1, na.locf, na.rm = FALSE))
dat
# id X2000 X2001 X2002 X2003 X2004
#1 A NA NA NA 400 400
#2 B NA 200 300 300 500
#3 C NA 80 80 70 70
#4 D NA NA 300 300 300
#5 E 100 205 205 205 205
#6 F NA 50 50 50 50
#7 G NA NA NA 600 600





share|improve this answer

















  • 1




    Note that we can use na.locf0 which defaults to na.rm=FALSE .
    – G. Grothendieck
    16 hours ago











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53402727%2ffill-row-values-to-the-right-of-some-value%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
1
down vote



accepted










Here's one way with dplyr and tidyr -



dat %>%
gather(year, value, -id) %>%
group_by(id) %>%
arrange(id, year) %>%
fill(value, .direction = "down") %>%
ungroup() %>%
spread(year, value)

# A tibble: 7 x 6
id X2000 X2001 X2002 X2003 X2004
<fct> <dbl> <dbl> <dbl> <dbl> <dbl>
1 A NA NA NA 400 400
2 B NA 200 300 300 500
3 C NA 80.0 80.0 70.0 70.0
4 D NA NA 300 300 300
5 E 100 205 205 205 205
6 F NA 50.0 50.0 50.0 50.0
7 G NA NA NA 600 600





share|improve this answer





















  • This was perfect
    – user1658170
    11 hours ago















up vote
1
down vote



accepted










Here's one way with dplyr and tidyr -



dat %>%
gather(year, value, -id) %>%
group_by(id) %>%
arrange(id, year) %>%
fill(value, .direction = "down") %>%
ungroup() %>%
spread(year, value)

# A tibble: 7 x 6
id X2000 X2001 X2002 X2003 X2004
<fct> <dbl> <dbl> <dbl> <dbl> <dbl>
1 A NA NA NA 400 400
2 B NA 200 300 300 500
3 C NA 80.0 80.0 70.0 70.0
4 D NA NA 300 300 300
5 E 100 205 205 205 205
6 F NA 50.0 50.0 50.0 50.0
7 G NA NA NA 600 600





share|improve this answer





















  • This was perfect
    – user1658170
    11 hours ago













up vote
1
down vote



accepted







up vote
1
down vote



accepted






Here's one way with dplyr and tidyr -



dat %>%
gather(year, value, -id) %>%
group_by(id) %>%
arrange(id, year) %>%
fill(value, .direction = "down") %>%
ungroup() %>%
spread(year, value)

# A tibble: 7 x 6
id X2000 X2001 X2002 X2003 X2004
<fct> <dbl> <dbl> <dbl> <dbl> <dbl>
1 A NA NA NA 400 400
2 B NA 200 300 300 500
3 C NA 80.0 80.0 70.0 70.0
4 D NA NA 300 300 300
5 E 100 205 205 205 205
6 F NA 50.0 50.0 50.0 50.0
7 G NA NA NA 600 600





share|improve this answer












Here's one way with dplyr and tidyr -



dat %>%
gather(year, value, -id) %>%
group_by(id) %>%
arrange(id, year) %>%
fill(value, .direction = "down") %>%
ungroup() %>%
spread(year, value)

# A tibble: 7 x 6
id X2000 X2001 X2002 X2003 X2004
<fct> <dbl> <dbl> <dbl> <dbl> <dbl>
1 A NA NA NA 400 400
2 B NA 200 300 300 500
3 C NA 80.0 80.0 70.0 70.0
4 D NA NA 300 300 300
5 E 100 205 205 205 205
6 F NA 50.0 50.0 50.0 50.0
7 G NA NA NA 600 600






share|improve this answer












share|improve this answer



share|improve this answer










answered 2 days ago









Shree

2,758321




2,758321












  • This was perfect
    – user1658170
    11 hours ago


















  • This was perfect
    – user1658170
    11 hours ago
















This was perfect
– user1658170
11 hours ago




This was perfect
– user1658170
11 hours ago












up vote
1
down vote













We could apply with na.locf



library(zoo)
dat[-1] <- t(apply(dat[-1], 1, na.locf, na.rm = FALSE))
dat
# id X2000 X2001 X2002 X2003 X2004
#1 A NA NA NA 400 400
#2 B NA 200 300 300 500
#3 C NA 80 80 70 70
#4 D NA NA 300 300 300
#5 E 100 205 205 205 205
#6 F NA 50 50 50 50
#7 G NA NA NA 600 600





share|improve this answer

















  • 1




    Note that we can use na.locf0 which defaults to na.rm=FALSE .
    – G. Grothendieck
    16 hours ago















up vote
1
down vote













We could apply with na.locf



library(zoo)
dat[-1] <- t(apply(dat[-1], 1, na.locf, na.rm = FALSE))
dat
# id X2000 X2001 X2002 X2003 X2004
#1 A NA NA NA 400 400
#2 B NA 200 300 300 500
#3 C NA 80 80 70 70
#4 D NA NA 300 300 300
#5 E 100 205 205 205 205
#6 F NA 50 50 50 50
#7 G NA NA NA 600 600





share|improve this answer

















  • 1




    Note that we can use na.locf0 which defaults to na.rm=FALSE .
    – G. Grothendieck
    16 hours ago













up vote
1
down vote










up vote
1
down vote









We could apply with na.locf



library(zoo)
dat[-1] <- t(apply(dat[-1], 1, na.locf, na.rm = FALSE))
dat
# id X2000 X2001 X2002 X2003 X2004
#1 A NA NA NA 400 400
#2 B NA 200 300 300 500
#3 C NA 80 80 70 70
#4 D NA NA 300 300 300
#5 E 100 205 205 205 205
#6 F NA 50 50 50 50
#7 G NA NA NA 600 600





share|improve this answer












We could apply with na.locf



library(zoo)
dat[-1] <- t(apply(dat[-1], 1, na.locf, na.rm = FALSE))
dat
# id X2000 X2001 X2002 X2003 X2004
#1 A NA NA NA 400 400
#2 B NA 200 300 300 500
#3 C NA 80 80 70 70
#4 D NA NA 300 300 300
#5 E 100 205 205 205 205
#6 F NA 50 50 50 50
#7 G NA NA NA 600 600






share|improve this answer












share|improve this answer



share|improve this answer










answered 2 days ago









akrun

390k13178251




390k13178251








  • 1




    Note that we can use na.locf0 which defaults to na.rm=FALSE .
    – G. Grothendieck
    16 hours ago














  • 1




    Note that we can use na.locf0 which defaults to na.rm=FALSE .
    – G. Grothendieck
    16 hours ago








1




1




Note that we can use na.locf0 which defaults to na.rm=FALSE .
– G. Grothendieck
16 hours ago




Note that we can use na.locf0 which defaults to na.rm=FALSE .
– G. Grothendieck
16 hours ago


















 

draft saved


draft discarded



















































 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53402727%2ffill-row-values-to-the-right-of-some-value%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Berounka

Different font size/position of beamer's navigation symbols template's content depending on regular/plain...

Sphinx de Gizeh