less than negative in R [closed] - r

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

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)

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

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(...)).

Calculation inaccuracy in date difference [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 4 years ago.
Improve this question
I tried to find the duration of the number of days of the starting date until the present time using Sys.time(). I use the following command to find the duration. However the output in R is totally wrong.
person$duration <- lubridate::interval(as.Date(person$create_date, "%m/%d/%y"),Sys.Date()) %/% days()
The output:
Name create_date duration
A 09/23/2014 -811
B 05/05/2014 -670
It is supposed to be 1380 days NOT -811. I am not sure why is it negative and why is it '-811' or '-670' specifically.
You were very close. Since your year consists of 4 digits, you need a capital Y.
library(lubridate)
interval(as.Date("09/23/2014", "%m/%d/%Y"),Sys.Date()) %/% days()
gives 1380.
In your code it took only the first 2 digits, and it assumed you wanted the current century, so year 2020. To be exact: in case you provide two numbers as a year, values between 69 and 99 are converted to 1969-1999, and values between 00 and 68 to 2000-2068.
interval(as.Date("09/23/2020", "%m/%d/%Y"),Sys.Date()) %/% days()
gives -811 as well.
Use the simple difference
as.numeric(as.Date("2018-07-05") - Sys.Date())
# use abs() if the date it's in the past

Resources