Vector analysis in R
up vote
1
down vote
favorite
As inputs your function should take a vector of 0s and 1s;
Every time you see a sequence of 1s in the data you need to increase the number of children by 1;
Be careful with the two subsequent sequences of 1s, where the difference between them is less than 5 (i.e. when there are less than 5 0s in between them, then it is the same child and not a new child);
To help you social planner provides some examples of what your function should return:
#Input: c(1,1,1,1,0,0,0,0)
#Output: 1 1 1 1 1 1 1 1
#Input: c(0,0,0,0,1,1,1,1,0,0,0,0,0,1,1,1)
#Output: 0 0 0 0 1 1 1 1 1 1 1 1 1 2 2 2
#Input: c(0,0,0,0,1,1,1,1,0,0,1,1,0,0,0,1,1,0,0,0,0,1,1,0,0,0,0,0,1)
#Output: 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2
Functions, which might be helpful:
diff()
cumsum()
which()
rle()
I dont quite understand how to approach the question, my thoughts on this are using diff function after the cumsum as it will help me to sustain a row of 1s but in this scenario i am loosing the length of vector (it obviously becomes shorter) also #rle$lenght
seems to help me to detect gaps of length 5 or more to turn 1s into 2s. Sorry for this question I am only a beginner
r rstudio
add a comment |
up vote
1
down vote
favorite
As inputs your function should take a vector of 0s and 1s;
Every time you see a sequence of 1s in the data you need to increase the number of children by 1;
Be careful with the two subsequent sequences of 1s, where the difference between them is less than 5 (i.e. when there are less than 5 0s in between them, then it is the same child and not a new child);
To help you social planner provides some examples of what your function should return:
#Input: c(1,1,1,1,0,0,0,0)
#Output: 1 1 1 1 1 1 1 1
#Input: c(0,0,0,0,1,1,1,1,0,0,0,0,0,1,1,1)
#Output: 0 0 0 0 1 1 1 1 1 1 1 1 1 2 2 2
#Input: c(0,0,0,0,1,1,1,1,0,0,1,1,0,0,0,1,1,0,0,0,0,1,1,0,0,0,0,0,1)
#Output: 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2
Functions, which might be helpful:
diff()
cumsum()
which()
rle()
I dont quite understand how to approach the question, my thoughts on this are using diff function after the cumsum as it will help me to sustain a row of 1s but in this scenario i am loosing the length of vector (it obviously becomes shorter) also #rle$lenght
seems to help me to detect gaps of length 5 or more to turn 1s into 2s. Sorry for this question I am only a beginner
r rstudio
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
As inputs your function should take a vector of 0s and 1s;
Every time you see a sequence of 1s in the data you need to increase the number of children by 1;
Be careful with the two subsequent sequences of 1s, where the difference between them is less than 5 (i.e. when there are less than 5 0s in between them, then it is the same child and not a new child);
To help you social planner provides some examples of what your function should return:
#Input: c(1,1,1,1,0,0,0,0)
#Output: 1 1 1 1 1 1 1 1
#Input: c(0,0,0,0,1,1,1,1,0,0,0,0,0,1,1,1)
#Output: 0 0 0 0 1 1 1 1 1 1 1 1 1 2 2 2
#Input: c(0,0,0,0,1,1,1,1,0,0,1,1,0,0,0,1,1,0,0,0,0,1,1,0,0,0,0,0,1)
#Output: 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2
Functions, which might be helpful:
diff()
cumsum()
which()
rle()
I dont quite understand how to approach the question, my thoughts on this are using diff function after the cumsum as it will help me to sustain a row of 1s but in this scenario i am loosing the length of vector (it obviously becomes shorter) also #rle$lenght
seems to help me to detect gaps of length 5 or more to turn 1s into 2s. Sorry for this question I am only a beginner
r rstudio
As inputs your function should take a vector of 0s and 1s;
Every time you see a sequence of 1s in the data you need to increase the number of children by 1;
Be careful with the two subsequent sequences of 1s, where the difference between them is less than 5 (i.e. when there are less than 5 0s in between them, then it is the same child and not a new child);
To help you social planner provides some examples of what your function should return:
#Input: c(1,1,1,1,0,0,0,0)
#Output: 1 1 1 1 1 1 1 1
#Input: c(0,0,0,0,1,1,1,1,0,0,0,0,0,1,1,1)
#Output: 0 0 0 0 1 1 1 1 1 1 1 1 1 2 2 2
#Input: c(0,0,0,0,1,1,1,1,0,0,1,1,0,0,0,1,1,0,0,0,0,1,1,0,0,0,0,0,1)
#Output: 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2
Functions, which might be helpful:
diff()
cumsum()
which()
rle()
I dont quite understand how to approach the question, my thoughts on this are using diff function after the cumsum as it will help me to sustain a row of 1s but in this scenario i am loosing the length of vector (it obviously becomes shorter) also #rle$lenght
seems to help me to detect gaps of length 5 or more to turn 1s into 2s. Sorry for this question I am only a beginner
r rstudio
r rstudio
asked Nov 11 at 8:50
Дмитрий Омегов
61
61
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
I make use of which function in r (https://www.r-bloggers.com/which-function-in-r/) and run length encoding (http://www.cookbook-r.com/Manipulating_data/Finding_sequences_of_identical_values/). Here's my attempt:
vector_analyse <- function(sample_vector){
# ----------------------------------------------------------------------------
# Signature: vector --> vector
# Author: kon_u
# Description: Given a sample vector of 0s and 1s, return a sequence of 1s in
# the data you need to increase the number of children by 1 (when there are less
# 5 0s in between them, then it is the same child and not a new child)
# ----------------------------------------------------------------------------
# ----------------------------------------------------------------------------
# Run Length Encoding gives a list of length and values
# ----------------------------------------------------------------------------
rle_object <- rle(sample_vector)
x <- rle_object$lengths # original length
y <- rle_object$values # original values
z <- which(y == 1) # index of 1 in vector y
if (length(z) == 1){
invisible()
} else{
for (i in 2:length(z)){
if (x[z[i]-1] >= 5){
y[z[i]] = y[z[i]]
} else {
y[z[i]] = y[z[i]] - 1
}
}
}
y_cumsum = cumsum(y)
rle_object$values <- y_cumsum
new_vector = inverse.rle(rle_object)
return(new_vector)
}
vector_analyse(c(1,1,1,1,0,0,0,0)) # 1 1 1 1 1 1 1 1
vector_analyse(c(0,0,0,0,1,1,1,1,0,0,0,0,0,1,1,1)) # 0 0 0 0 1 1 1 1 1 1 1 1 1 2 2 2
vector_analyse(c(0,0,0,0,1,1,1,1,0,0,1,1,0,0,0,1,1,0,0,0,0,1,1,0,0,0,0,0,1)) # 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
I make use of which function in r (https://www.r-bloggers.com/which-function-in-r/) and run length encoding (http://www.cookbook-r.com/Manipulating_data/Finding_sequences_of_identical_values/). Here's my attempt:
vector_analyse <- function(sample_vector){
# ----------------------------------------------------------------------------
# Signature: vector --> vector
# Author: kon_u
# Description: Given a sample vector of 0s and 1s, return a sequence of 1s in
# the data you need to increase the number of children by 1 (when there are less
# 5 0s in between them, then it is the same child and not a new child)
# ----------------------------------------------------------------------------
# ----------------------------------------------------------------------------
# Run Length Encoding gives a list of length and values
# ----------------------------------------------------------------------------
rle_object <- rle(sample_vector)
x <- rle_object$lengths # original length
y <- rle_object$values # original values
z <- which(y == 1) # index of 1 in vector y
if (length(z) == 1){
invisible()
} else{
for (i in 2:length(z)){
if (x[z[i]-1] >= 5){
y[z[i]] = y[z[i]]
} else {
y[z[i]] = y[z[i]] - 1
}
}
}
y_cumsum = cumsum(y)
rle_object$values <- y_cumsum
new_vector = inverse.rle(rle_object)
return(new_vector)
}
vector_analyse(c(1,1,1,1,0,0,0,0)) # 1 1 1 1 1 1 1 1
vector_analyse(c(0,0,0,0,1,1,1,1,0,0,0,0,0,1,1,1)) # 0 0 0 0 1 1 1 1 1 1 1 1 1 2 2 2
vector_analyse(c(0,0,0,0,1,1,1,1,0,0,1,1,0,0,0,1,1,0,0,0,0,1,1,0,0,0,0,0,1)) # 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2
add a comment |
up vote
0
down vote
I make use of which function in r (https://www.r-bloggers.com/which-function-in-r/) and run length encoding (http://www.cookbook-r.com/Manipulating_data/Finding_sequences_of_identical_values/). Here's my attempt:
vector_analyse <- function(sample_vector){
# ----------------------------------------------------------------------------
# Signature: vector --> vector
# Author: kon_u
# Description: Given a sample vector of 0s and 1s, return a sequence of 1s in
# the data you need to increase the number of children by 1 (when there are less
# 5 0s in between them, then it is the same child and not a new child)
# ----------------------------------------------------------------------------
# ----------------------------------------------------------------------------
# Run Length Encoding gives a list of length and values
# ----------------------------------------------------------------------------
rle_object <- rle(sample_vector)
x <- rle_object$lengths # original length
y <- rle_object$values # original values
z <- which(y == 1) # index of 1 in vector y
if (length(z) == 1){
invisible()
} else{
for (i in 2:length(z)){
if (x[z[i]-1] >= 5){
y[z[i]] = y[z[i]]
} else {
y[z[i]] = y[z[i]] - 1
}
}
}
y_cumsum = cumsum(y)
rle_object$values <- y_cumsum
new_vector = inverse.rle(rle_object)
return(new_vector)
}
vector_analyse(c(1,1,1,1,0,0,0,0)) # 1 1 1 1 1 1 1 1
vector_analyse(c(0,0,0,0,1,1,1,1,0,0,0,0,0,1,1,1)) # 0 0 0 0 1 1 1 1 1 1 1 1 1 2 2 2
vector_analyse(c(0,0,0,0,1,1,1,1,0,0,1,1,0,0,0,1,1,0,0,0,0,1,1,0,0,0,0,0,1)) # 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2
add a comment |
up vote
0
down vote
up vote
0
down vote
I make use of which function in r (https://www.r-bloggers.com/which-function-in-r/) and run length encoding (http://www.cookbook-r.com/Manipulating_data/Finding_sequences_of_identical_values/). Here's my attempt:
vector_analyse <- function(sample_vector){
# ----------------------------------------------------------------------------
# Signature: vector --> vector
# Author: kon_u
# Description: Given a sample vector of 0s and 1s, return a sequence of 1s in
# the data you need to increase the number of children by 1 (when there are less
# 5 0s in between them, then it is the same child and not a new child)
# ----------------------------------------------------------------------------
# ----------------------------------------------------------------------------
# Run Length Encoding gives a list of length and values
# ----------------------------------------------------------------------------
rle_object <- rle(sample_vector)
x <- rle_object$lengths # original length
y <- rle_object$values # original values
z <- which(y == 1) # index of 1 in vector y
if (length(z) == 1){
invisible()
} else{
for (i in 2:length(z)){
if (x[z[i]-1] >= 5){
y[z[i]] = y[z[i]]
} else {
y[z[i]] = y[z[i]] - 1
}
}
}
y_cumsum = cumsum(y)
rle_object$values <- y_cumsum
new_vector = inverse.rle(rle_object)
return(new_vector)
}
vector_analyse(c(1,1,1,1,0,0,0,0)) # 1 1 1 1 1 1 1 1
vector_analyse(c(0,0,0,0,1,1,1,1,0,0,0,0,0,1,1,1)) # 0 0 0 0 1 1 1 1 1 1 1 1 1 2 2 2
vector_analyse(c(0,0,0,0,1,1,1,1,0,0,1,1,0,0,0,1,1,0,0,0,0,1,1,0,0,0,0,0,1)) # 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2
I make use of which function in r (https://www.r-bloggers.com/which-function-in-r/) and run length encoding (http://www.cookbook-r.com/Manipulating_data/Finding_sequences_of_identical_values/). Here's my attempt:
vector_analyse <- function(sample_vector){
# ----------------------------------------------------------------------------
# Signature: vector --> vector
# Author: kon_u
# Description: Given a sample vector of 0s and 1s, return a sequence of 1s in
# the data you need to increase the number of children by 1 (when there are less
# 5 0s in between them, then it is the same child and not a new child)
# ----------------------------------------------------------------------------
# ----------------------------------------------------------------------------
# Run Length Encoding gives a list of length and values
# ----------------------------------------------------------------------------
rle_object <- rle(sample_vector)
x <- rle_object$lengths # original length
y <- rle_object$values # original values
z <- which(y == 1) # index of 1 in vector y
if (length(z) == 1){
invisible()
} else{
for (i in 2:length(z)){
if (x[z[i]-1] >= 5){
y[z[i]] = y[z[i]]
} else {
y[z[i]] = y[z[i]] - 1
}
}
}
y_cumsum = cumsum(y)
rle_object$values <- y_cumsum
new_vector = inverse.rle(rle_object)
return(new_vector)
}
vector_analyse(c(1,1,1,1,0,0,0,0)) # 1 1 1 1 1 1 1 1
vector_analyse(c(0,0,0,0,1,1,1,1,0,0,0,0,0,1,1,1)) # 0 0 0 0 1 1 1 1 1 1 1 1 1 2 2 2
vector_analyse(c(0,0,0,0,1,1,1,1,0,0,1,1,0,0,0,1,1,0,0,0,0,1,1,0,0,0,0,0,1)) # 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2
answered Nov 12 at 17:42
kon_u
1616
1616
add a comment |
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%2f53247149%2fvector-analysis-in-r%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