R fill vector with same value until a different one is found [duplicate]
This question already has an answer here:
Replacing NAs with latest non-NA value
13 answers
In R I have two vectors such as
2 NA NA 1 NA NA
1 NA 2 NA NA NA
And I would like them to be like
2 2 2 1 1 1
1 1 2 2 2 2
Any ideas?
r vector fill
marked as duplicate by Sotos
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
Nov 15 '18 at 14:37
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
This question already has an answer here:
Replacing NAs with latest non-NA value
13 answers
In R I have two vectors such as
2 NA NA 1 NA NA
1 NA 2 NA NA NA
And I would like them to be like
2 2 2 1 1 1
1 1 2 2 2 2
Any ideas?
r vector fill
marked as duplicate by Sotos
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
Nov 15 '18 at 14:37
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
2
Seezoo::na.locf
from thezoo
package.
– nicola
Nov 15 '18 at 14:21
add a comment |
This question already has an answer here:
Replacing NAs with latest non-NA value
13 answers
In R I have two vectors such as
2 NA NA 1 NA NA
1 NA 2 NA NA NA
And I would like them to be like
2 2 2 1 1 1
1 1 2 2 2 2
Any ideas?
r vector fill
This question already has an answer here:
Replacing NAs with latest non-NA value
13 answers
In R I have two vectors such as
2 NA NA 1 NA NA
1 NA 2 NA NA NA
And I would like them to be like
2 2 2 1 1 1
1 1 2 2 2 2
Any ideas?
This question already has an answer here:
Replacing NAs with latest non-NA value
13 answers
r vector fill
r vector fill
asked Nov 15 '18 at 14:20
OscarOscar
9019
9019
marked as duplicate by Sotos
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
Nov 15 '18 at 14:37
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by Sotos
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
Nov 15 '18 at 14:37
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
2
Seezoo::na.locf
from thezoo
package.
– nicola
Nov 15 '18 at 14:21
add a comment |
2
Seezoo::na.locf
from thezoo
package.
– nicola
Nov 15 '18 at 14:21
2
2
See
zoo::na.locf
from the zoo
package.– nicola
Nov 15 '18 at 14:21
See
zoo::na.locf
from the zoo
package.– nicola
Nov 15 '18 at 14:21
add a comment |
2 Answers
2
active
oldest
votes
With tidyverse
:
read.table(text="2 NA NA 1 NA NA
1 NA 2 NA NA NA")%>%
t()%>%
data.frame()%>%
mutate_all(funs(as.numeric(.)))%>%
fill(c(X1,X2))%>%t
[,1] [,2] [,3] [,4] [,5] [,6]
X1 2 2 2 1 1 1
X2 1 1 2 2 2 2
add a comment |
vectors:
a <- c(2, NA, NA, 1, NA, NA)
b <- c(1, NA, 2, NA, NA, NA)
As anonymous function:
(function(x)
nai <- which(is.na(x))
i <- which(!is.na(x))
x[nai] <- x[i][findInterval(nai,i)]
return(x)
)
Call:
(function(x)
nai <- which(is.na(x))
i <- which(!is.na(x))
x[nai] <- x[i][findInterval(nai,i)]
return(x)
)(a)
#[1] 2 2 2 1 1 1
(function(x)
nai <- which(is.na(x))
i <- which(!is.na(x))
x[nai] <- x[i][findInterval(nai,i)]
return(x)
)(b)
#[1] 1 1 2 2 2 2
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
With tidyverse
:
read.table(text="2 NA NA 1 NA NA
1 NA 2 NA NA NA")%>%
t()%>%
data.frame()%>%
mutate_all(funs(as.numeric(.)))%>%
fill(c(X1,X2))%>%t
[,1] [,2] [,3] [,4] [,5] [,6]
X1 2 2 2 1 1 1
X2 1 1 2 2 2 2
add a comment |
With tidyverse
:
read.table(text="2 NA NA 1 NA NA
1 NA 2 NA NA NA")%>%
t()%>%
data.frame()%>%
mutate_all(funs(as.numeric(.)))%>%
fill(c(X1,X2))%>%t
[,1] [,2] [,3] [,4] [,5] [,6]
X1 2 2 2 1 1 1
X2 1 1 2 2 2 2
add a comment |
With tidyverse
:
read.table(text="2 NA NA 1 NA NA
1 NA 2 NA NA NA")%>%
t()%>%
data.frame()%>%
mutate_all(funs(as.numeric(.)))%>%
fill(c(X1,X2))%>%t
[,1] [,2] [,3] [,4] [,5] [,6]
X1 2 2 2 1 1 1
X2 1 1 2 2 2 2
With tidyverse
:
read.table(text="2 NA NA 1 NA NA
1 NA 2 NA NA NA")%>%
t()%>%
data.frame()%>%
mutate_all(funs(as.numeric(.)))%>%
fill(c(X1,X2))%>%t
[,1] [,2] [,3] [,4] [,5] [,6]
X1 2 2 2 1 1 1
X2 1 1 2 2 2 2
answered Nov 15 '18 at 14:29
jyjekjyjek
1,504312
1,504312
add a comment |
add a comment |
vectors:
a <- c(2, NA, NA, 1, NA, NA)
b <- c(1, NA, 2, NA, NA, NA)
As anonymous function:
(function(x)
nai <- which(is.na(x))
i <- which(!is.na(x))
x[nai] <- x[i][findInterval(nai,i)]
return(x)
)
Call:
(function(x)
nai <- which(is.na(x))
i <- which(!is.na(x))
x[nai] <- x[i][findInterval(nai,i)]
return(x)
)(a)
#[1] 2 2 2 1 1 1
(function(x)
nai <- which(is.na(x))
i <- which(!is.na(x))
x[nai] <- x[i][findInterval(nai,i)]
return(x)
)(b)
#[1] 1 1 2 2 2 2
add a comment |
vectors:
a <- c(2, NA, NA, 1, NA, NA)
b <- c(1, NA, 2, NA, NA, NA)
As anonymous function:
(function(x)
nai <- which(is.na(x))
i <- which(!is.na(x))
x[nai] <- x[i][findInterval(nai,i)]
return(x)
)
Call:
(function(x)
nai <- which(is.na(x))
i <- which(!is.na(x))
x[nai] <- x[i][findInterval(nai,i)]
return(x)
)(a)
#[1] 2 2 2 1 1 1
(function(x)
nai <- which(is.na(x))
i <- which(!is.na(x))
x[nai] <- x[i][findInterval(nai,i)]
return(x)
)(b)
#[1] 1 1 2 2 2 2
add a comment |
vectors:
a <- c(2, NA, NA, 1, NA, NA)
b <- c(1, NA, 2, NA, NA, NA)
As anonymous function:
(function(x)
nai <- which(is.na(x))
i <- which(!is.na(x))
x[nai] <- x[i][findInterval(nai,i)]
return(x)
)
Call:
(function(x)
nai <- which(is.na(x))
i <- which(!is.na(x))
x[nai] <- x[i][findInterval(nai,i)]
return(x)
)(a)
#[1] 2 2 2 1 1 1
(function(x)
nai <- which(is.na(x))
i <- which(!is.na(x))
x[nai] <- x[i][findInterval(nai,i)]
return(x)
)(b)
#[1] 1 1 2 2 2 2
vectors:
a <- c(2, NA, NA, 1, NA, NA)
b <- c(1, NA, 2, NA, NA, NA)
As anonymous function:
(function(x)
nai <- which(is.na(x))
i <- which(!is.na(x))
x[nai] <- x[i][findInterval(nai,i)]
return(x)
)
Call:
(function(x)
nai <- which(is.na(x))
i <- which(!is.na(x))
x[nai] <- x[i][findInterval(nai,i)]
return(x)
)(a)
#[1] 2 2 2 1 1 1
(function(x)
nai <- which(is.na(x))
i <- which(!is.na(x))
x[nai] <- x[i][findInterval(nai,i)]
return(x)
)(b)
#[1] 1 1 2 2 2 2
answered Nov 15 '18 at 14:35
Andre ElricoAndre Elrico
5,73311229
5,73311229
add a comment |
add a comment |
2
See
zoo::na.locf
from thezoo
package.– nicola
Nov 15 '18 at 14:21