Fill row values to the right of some value
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
add a comment |
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
Related posts.cbind(dat[1], t(zoo::na.locf(t(dat[-1]))))
– Henrik
Nov 20 at 23:09
add a comment |
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
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
r
asked Nov 20 at 22:46
user1658170
1691617
1691617
Related posts.cbind(dat[1], t(zoo::na.locf(t(dat[-1]))))
– Henrik
Nov 20 at 23:09
add a comment |
Related posts.cbind(dat[1], t(zoo::na.locf(t(dat[-1]))))
– Henrik
Nov 20 at 23:09
Related posts.
cbind(dat[1], t(zoo::na.locf(t(dat[-1]))))
– Henrik
Nov 20 at 23:09
Related posts.
cbind(dat[1], t(zoo::na.locf(t(dat[-1]))))
– Henrik
Nov 20 at 23:09
add a comment |
2 Answers
2
active
oldest
votes
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
This was perfect
– user1658170
Nov 22 at 18:42
add a comment |
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
1
Note that we can usena.locf0
which defaults tona.rm=FALSE
.
– G. Grothendieck
Nov 22 at 13:50
add a comment |
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',
autoActivateHeartbeat: false,
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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
This was perfect
– user1658170
Nov 22 at 18:42
add a comment |
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
This was perfect
– user1658170
Nov 22 at 18:42
add a comment |
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
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
answered Nov 20 at 23:01
Shree
3,2861323
3,2861323
This was perfect
– user1658170
Nov 22 at 18:42
add a comment |
This was perfect
– user1658170
Nov 22 at 18:42
This was perfect
– user1658170
Nov 22 at 18:42
This was perfect
– user1658170
Nov 22 at 18:42
add a comment |
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
1
Note that we can usena.locf0
which defaults tona.rm=FALSE
.
– G. Grothendieck
Nov 22 at 13:50
add a comment |
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
1
Note that we can usena.locf0
which defaults tona.rm=FALSE
.
– G. Grothendieck
Nov 22 at 13:50
add a comment |
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
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
answered Nov 21 at 5:06
akrun
396k13187260
396k13187260
1
Note that we can usena.locf0
which defaults tona.rm=FALSE
.
– G. Grothendieck
Nov 22 at 13:50
add a comment |
1
Note that we can usena.locf0
which defaults tona.rm=FALSE
.
– G. Grothendieck
Nov 22 at 13:50
1
1
Note that we can use
na.locf0
which defaults to na.rm=FALSE
.– G. Grothendieck
Nov 22 at 13:50
Note that we can use
na.locf0
which defaults to na.rm=FALSE
.– G. Grothendieck
Nov 22 at 13:50
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
Related posts.
cbind(dat[1], t(zoo::na.locf(t(dat[-1]))))
– Henrik
Nov 20 at 23:09