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
Related
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)
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(...)).
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 5 years ago.
Improve this question
I have my data in xls file.I try to read like this
> df = read.xls ("natgas.xls")
Output
df
Dec.2007 X2399154
1 Jan-2008 2733970
2 Feb-2008 2503421
3 Mar-2008 2278151
4 Apr-2008 1823867
5 May-2008 1576387
6 Jun-2008 1604249
7 Jul-2008 1708641
8 Aug-2008 1682924
9 Sep-2008 1460924
10 Oct-2008 1635827
Everything is OK,except the first line.
When I index second column
> df[,2]
[1] 2733970 2503421 2278151 1823867 1576387 1604249 1708641 1682924 1460924
the first value is missing.
How to solve this?
Looks like you need to add header = FALSE to your read.xls call (which seems to come from the gdata package):
df1 <- read.xls("natgas.xls", header = FALSE)
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 5 years ago.
Improve this question
I have a text file myfile.txt that looks like this:
10
20
30
40
I try to load it in R using:
nums <- read.csv('myfile.txt', header=FALSE);
However, this returns a list. What I want is the equivalent of:
nums <- c(10, 20, 30, 40);
I later have code that does the following:
v = sprintf("%d", nums);
This works fine when I have the vector version, but when I try to load my data from file, I get the following error:
Error in sprintf("%d", nums) : unsupported type
Execution halted
Does nobody use scan anymore??
> scan("./myfile.txt")
Read 4 items
[1] 10 20 30 4
I mistyped the fourth item in the file.
Returns exactly what you wanted:
> n = scan("./myfile.txt")
Read 4 items
> identical(n, c(10,20,30,4))
[1] TRUE
When you use read.csv, the output should be a data.frame.
If you want to access the column of integers, try nums[[1]]. (With nums being the output read.csv.)
To convert to a vector, use unlist(nums).
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