How to print out an odd integers 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 1 year ago.
Improve this question
for (i in 1:10) print(i)
for (i in 1:10) if .... ??
what is the right expression to specify an odd numbers ?

One possibility if for some reason you must avoid using seq()
for (i in 1:10) {
if (i %% 2 ==1) print(i)
}
#[1] 1
#[1] 3
#[1] 5
#[1] 7
#[1] 9

Related

R basics and questions [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 6 months ago.
Improve this question
I have assigned a value for a variable but still am getting an error message. Kindly help me rectify
Code:
n <- 1000
x <- seq(1,n)
sum(x)
Error: object 'x' not found
You either need to use a new line for each command or end each command with semicolon (";")
n <- 1000
x <- seq(1,n)
sum(x)
Or:
n <- 1000; x <- seq(1,n); sum(x)

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

If statement in R, multiple conditions [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 wonder how I can write this if statement correct. I have tried a lot of things, but none of them worked.
b <- matrix(NA,10,10)
> for (row in 1:10)
>> for (column in 1:10)
>>> if(!is.na(a[row,column] && a==(1 || 2 || 3))
b[row,column]==1
>>> else
b[row,column]==0
The problem is here:
if(!is.na(a[row,column] && a==(1 || 2 || 3))
Assuming that 'a' is a matrix with the same dimension as 'b', we can do this without any if/else
+((a %in% 1:3) & !is.na(a))
data
set.seed(24)
a <- matrix(sample(c(1:9, NA), 10*10, replace = TRUE), 10, 10)

First row not detected with 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 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)

how may I remove elements in a string vector by elements in another string vector [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 5 years ago.
Improve this question
setdiff(c("a","c"),c("a","b","c","d"))
#character(0)
This code is supposed to return a vector of c("b","d"), what's wrong with it?
setdiff is asymmetric, as the help page warns about (though subtly).
This works as you expect,
> setdiff(c("a","b","c","d"),c("a","c"))
[1] "b" "d"
A simple function works either way,
setdiff2 <- function(x,y){
d1 <- setdiff(x,y)
d2 <- setdiff(y,x)
if(length(d2) > length(d1))
return(d2)
else
return(d1)
}
> setdiff2(c("a","c"), c("a","b","c","d"))
[1] "b" "d"

Resources