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.
r
add a comment |
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.
r
Related posts.cbind(dat[1], t(zoo::na.locf(t(dat[-1]))))
– Henrik
2 days ago
add a comment |
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.
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 2 days ago
user1658170
1711617
1711617
Related posts.cbind(dat[1], t(zoo::na.locf(t(dat[-1]))))
– Henrik
2 days ago
add a comment |
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
add a comment |
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
This was perfect
– user1658170
11 hours ago
add a comment |
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
1
Note that we can usena.locf0
which defaults tona.rm=FALSE
.
– G. Grothendieck
16 hours ago
add a comment |
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
This was perfect
– user1658170
11 hours ago
add a comment |
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
This was perfect
– user1658170
11 hours ago
add a comment |
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
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 2 days ago
Shree
2,758321
2,758321
This was perfect
– user1658170
11 hours ago
add a comment |
This was perfect
– user1658170
11 hours ago
This was perfect
– user1658170
11 hours ago
This was perfect
– user1658170
11 hours ago
add a comment |
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
1
Note that we can usena.locf0
which defaults tona.rm=FALSE
.
– G. Grothendieck
16 hours ago
add a comment |
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
1
Note that we can usena.locf0
which defaults tona.rm=FALSE
.
– G. Grothendieck
16 hours ago
add a comment |
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
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 2 days ago
akrun
390k13178251
390k13178251
1
Note that we can usena.locf0
which defaults tona.rm=FALSE
.
– G. Grothendieck
16 hours ago
add a comment |
1
Note that we can usena.locf0
which defaults tona.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
add a comment |
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
2 days ago