I'm learning R and I face a problem I don't know how to solve. I have an input subset similar to the following, with a client_id and 7 integers:
(client_id, 10, 8, -5, 8, 1, -23, 12)
I would like return the same vector but with an additional fields. First, one containing the number of times any of the other values are negative. For the above example, the result would be:
(client_id, 10, 8, -5, 8, 1, -23, 12, 2)
because there are just 2 negative numbers in the 7 integers.
A second field would be the number of values that are 0
(client_id, 10, 8, -5, 8, 1, -23, 12, 2, 3)
because there are 3 values between 0 and 10.
Can anyone help me with this issue?
Thanks a lot.
How about this:
> t <- c("client_id", 10, 8, -5, 8, 1, -23, 12) # create vector
> nums <- as.numeric(t[2:length(t)]) # get numbers only from vector
> sum(nums < 0) # Counts the numbers less that 0
[1] 2
> sum(nums > 0 & nums < 10) # counts the number > 0 and < 10
[1] 3
> t <- append(t,sum(nums < 0)) # append to original vector
> t <- append(t,sum(nums > 0 & nums < 10))
> t
[1] "client_id" "10" "8" "-5" "8" "1" "-23" "12" "2" "3"
>
If your vector is x it would be
x <- c(x, length(which(x[-1]<0)), length(which(x[-1]>=0 & x[-1]<=10)))
To answer to #Nishanth : you can also do
x <- c(x, sum(x[-1]<0), sum(x[-1]>=0 && x[-1]<=10))
Related
I have a question I have the following data
c(1, 2, 4, 5, 1, 8, 9)
I set a l = 2 and an u = 6
I want to find all the values in the range (3,7)
How can I do this?
In base R we can use comparison operators to create a logical vector and use that for subsetting the original vector
x[x > 2 & x <= 6]
#[1] 3 5 6
Or using a for loop, initialize an empty vector, loop through the elements of 'x', if the value is between 2 and 6, then concatenate that value to the empty vector
v1 <- c()
for(i in x) {
if(i > 2 & i <= 6) v1 <- c(v1, i)
}
v1
#[1] 3 5 6
data
x <- c(3, 5, 6, 8, 1, 2, 1)
If I have a sorted vector, like
vec <- c(5, 6, 7, 8, 9, 10, 11, 12, 13, 14)
and I have
x <- 9.5
Then x is between the 5th and 6th value in my sorted row, and I want to get the index 5. How can I do it?
The following will give the result you're looking for:
x<-c(5, 6, 7, 8, 9, 10, 11, 12, 13, 14)
findInterval(9.5,x)
> [1] 5
Alternative solutions include:
> max(which(x < 9.5))
[1] 5
There should be multiple ways to do this. One way using which.max
which.max(vec > x) - 1
#[1] 5
This finds first index where vec is greater than x and then returns an index 1 less than that.
As it is sorted the opposite should work as well
which.min(vec < x) - 1
#[1] 5
I have a sequence say: 11, 11, 6, 4, 11, 10, 6,... which simulates the following possible card values for a player's hand in blackjack.
When the sum of these numbers accumulates to 11 or greater, I am trying to make each value of 11 which occurs after this point equal to 1.
Where the cumulative sum is: 11, 22, 28, 32,...
Desired outcome: 11, 1, 6, 4, 1, 10, 6,...
Here is what I have been unsuccessfully trying with:
nphand = c(11,11,6,4,11,10,6)
v=cumsum(nphand)
p=v[v<=11]
for (i in (length(p)+1):length(nphand)){
if (nphand[i]==11){
nphand[i]==1
}
}
Any help and/or advice would be greatly appreciated.
This should work.
nphand = c(11,11,6,4,11,10,6)
v=cumsum(nphand)
p=v[v<=11]
for (i in 1:length(nphand)){
cards <- nphand[1:i]
elevens <- cards[-1] %in% 11
if(sum(cards)>=11 & sum(elevens) >=1){
cards[which(elevens) +1] <- 1
}
nphand[1:i] <- cards
}
> nphand
[1] 11 1 6 4 1 10 6
This looks like it is dependent on the first card being 11. The solution below should work regardless:
nphand = c(2,11,6,4,11,10,6)
v=cumsum(nphand)
p=v[v<=11]
for (i in 1:length(nphand)){
cards <- nphand[1:i]
elevens <- cards %in% 11
if(sum(cards)>=11 & sum(elevens) >=1){
cards[which(elevens[-1]) + 1] <- 1
}
nphand[1:i] <- cards
}
I have a vector:
as <- c(1,2,3,4,5,9)
I need to extract the first continunous sequence in the vector, starting at index 1, such that the output is the following:
1 2 3 4 5
Is there a smart function for doing this, or do I have to do something not so elegant like this:
a <- c(1,2,3,4,5,9)
is_continunous <- c()
for (i in 1:length(a)) {
if(a[i+1] - a[i] == 1) {
is_continunous <- c(is_continunous, i)
} else {
break
}
}
continunous_numbers <- c()
if(is_continunous[1] == 1) {
is_continunous <- c(is_continunous, length(is_continunous)+1)
continunous_numbers <- a[is_continunous]
}
It does the trick, but I would expect that there is a function that can already do this.
It isn't clear what you need if the index of the continuous sequence only if it starts at index one or the first sequence, whatever the beginning index is.
In both case, you need to start by checking the difference between adjacent elements:
d_as <- diff(as)
If you need the first sequence only if it starts at index 1:
if(d_as[1]==1) 1:(rle(d_as)$lengths[1]+1) else NULL
# [1] 1 2 3 4 5
rle permits to know lengths and values for each consecutive sequence of same value.
If you need the first continuous sequence, whatever the starting index is:
rle_d_as <- rle(d_as)
which(d_as==1)[1]+(0:(rle_d_as$lengths[rle_d_as$values==1][1]))
Examples (for the second option):
as <- c(1,2,3,4,5,9)
d_as <- diff(as)
rle_d_as <- rle(d_as)
which(d_as==1)[1]+(0:(rle_d_as$lengths[rle_d_as$values==1][1]))
#[1] 1 2 3 4 5
as <- c(4,3,1,2,3,4,5,9)
d_as <- diff(as)
rle_d_as <- rle(d_as)
which(d_as==1)[1]+(0:(rle_d_as$lengths[rle_d_as$values==1][1]))
# [1] 3 4 5 6 7
as <- c(1, 2, 3, 6, 7, 8)
d_as <- diff(as)
rle_d_as <- rle(d_as)
which(d_as==1)[1]+(0:(rle_d_as$lengths[rle_d_as$values==1][1]))
# [1] 1 2 3
A simple way to catch the sequence would be to find the diff of your vector and grab all elements with diff == 1 plus the very next element, i.e.
d1<- which(diff(as) == 1)
as[c(d1, d1[length(d1)]+1)]
NOTE
This will only work If you only have one sequence in your vector. However If we want to make it more general, then I 'd suggest creating a function as so,
get_seq <- function(vec){
d1 <- which(diff(as) == 1)
if(all(diff(d1) == 1)){
return(c(d1, d1[length(d1)]+1))
}else{
d2 <- split(d1, cumsum(c(1, diff(d1) != 1)))[[1]]
return(c(d2, d2[length(d2)]+1))
}
}
#testing it
as <- c(3, 5, 1, 2, 3, 4, 9, 7, 5, 4, 5, 6, 7, 8)
get_seq(as)
#[1] 3 4 5 6
as <- c(8, 9, 10, 11, 1, 2, 3, 4, 7, 8, 9, 10)
get_seq(as)
#[1] 1 2 3 4
as <- c(1, 2, 3, 4, 5, 6, 11)
get_seq(as)
#[1] 1 2 3 4 5 6
I want to randomly pick a number from a vector with 8 elements that sums to 35. If the number is 0 look for another number. If the number is greater than 0, make this number -1. Do this in a loop until the sum of the vector is 20. How can I do this in R?
For example: vec<-c(2,3,6,0,8,5,6,5)
Pick a number from this list randomly and make the number -1 until the sum of the elements becomes 20.
I'm really really not sure that is what you want, but for what I understand of your question, here is my solution. You'll get most of the concept and key fonctions in my script. Use that and help() to understand them and optimize it.
vec <- c(2, 3, 6, 0, 8, 5, 6, 5)
summ <- 0
new.vec <- NULL
iter <- 1
while(summ<20) {
selected <- sample(vec,1)
if(selected!=0) new.vec[iter] <- selected-1
summ <- sum(new.vec)
iter <- iter+1
}
Try this:
vec <- c(2, 3, 6, 0, 8, 5, 6, 5)
#just setting the seed for reproducibility
set.seed(19)
tabulate(sample(rep(seq_along(vec),vec),20))
#[1] 0 2 4 0 4 5 3 2