generate a matrix with random values specified in a interval [closed] - r

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 4 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
I am trying to generate a matrix with random values between 1 to 10 having 2 columns and rows equal to days. But with the below code, I am getting same values of random number in alternate rows like 2 2, 4 4, 2 2, 4 4 and so on.
days<-10
matrix1<-matrix(round(runif(days,1,10)),nrow = days,ncol = 2)

You need
matrix1<-matrix(round(runif(2*days,1,10)),nrow = days,ncol = 2)
Currently, the line runif(days,1,10)) only requests 10 values, but you want 20 values. matrix() just recycles the 10 values since it expects 20.
Also, if you want the numbers 1-10 in equal proportion, you should use sample(1:10, 2*days, replace = TRUE) instead of round(runif(...)).

Related

In R, How to count the total number of items in a vector greater than the absolute value of 1 [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
I want to count the total number of items in a vector greater than (>) the absolute value of 1.
vec <- c(5,3,-7,0,0,0,-1,0,3,1,-3,4,7)
the result should exclude 0, 1 and -1 in the count and return the total count of 7
attempt
sum(vec >abs(1))
# this returns '5' instead of '7'
Thanks
The abs should be on the 'vec' and not on 1
sum(abs(vec) > 1)
[1] 7

Why means are different in Rstudio? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
I am using mean function in R. But getting different answers all the time, I change the sequence of the inputs.
Not able to understand. Please help me.
Thanks in advance.
> mean(10,12,13)
[1] 10
> mean(12,10,13)
[1] 12
> mean(13,10,12)
[1] 13
You should pass numbers as a vector to mean and not as separate values. See ?mean.
mean(x, ...)
So when you are doing mean(10,12,13) you are just getting mean of 10 hence the same number is returned. Same with mean(12,10,13).
Pass them as a vector with c(...).
mean(c(10,12,13))
#[1] 11.66667
mean(c(12,10,13))
#[1] 11.66667
This is different behaviour from functions like sum/min/max where you can pass different numbers as comma separated values.
sum(10, 12, 13)
#[1] 35
min(10, 12, 9)
#[1] 9

Make the Day of Week Variable Binary with Baseline [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
I want to make the day of the week variable binary, but also have a baseline which should be Saturday.
I have tried the following, but this does not give me my intended result at all. Is there a need to use a logical?
The range of the days of the week is 1 to 7, where 1 would be Monday.
First i renamed the numbers into the days of the week.
df$DayofW <- recode(df$dowc,
"1"="Monday",
"2"="Tuesday",
"3"="Wednesday",
"4"="Thursday",
"5"="Friday",
"6"="Saturday",
"7"="Sunday")
df$DayofW <- ifelse((df$dowc == 6), 1, -1)
The issue is that we are assigning (=) instead of comparing (==). According to ?ifelse, the first argument test
test - an object which can be coerced to logical mode.
So, it needs a comparison operator
ifelse((df$dowc == 6), 1, -1)

less than negative in R [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
How can I compare a column in data frame for range? The range is like less than a negative number and greater than a positive number. For Positive number there is no problem but for negative number it is taking it as an assignment operator.Code for reference is given below
Resited<-Reap[mean < -5 & mean > 5,]
"mean" cannot be less than -5 and more than 5 at the same time. Did you mean logical OR? If both abs values are the same, you could simply write
Resited <- Reap[abs(mean) > 5, ]
This simplest way in my opinion is just to put parentheses around your negative value:
Resited<-Reap[mean<(-5) | mean>5,]
For the case where your range is not symmetric around zero, R allows you to define your own operators:
`%between%` = function(x,range) x>range[1] & x<range[2]
-6 %between% c(-7,-2)
[1] TRUE

How to multiply index number to index value in a vector in R [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Q <- c(1,2,3,4)
I want to make it so that every value in the vector gets multiplied by it's vector number. Such that 1*1, 2*2, 3*3, 4*4
Try:
Q * seq_along(Q)
#[1] 1 4 9 16
One way is:
>Q*1:length(Q)
#[1] 1 4 9 16

Resources