If statement in R, multiple conditions [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 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)

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)

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)

Order of operations in summarise [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
What is happening in the first line of code and why does the result differ from the two next results?
library(tidyverse)
library(magrittr)
data.frame(A=c(2,2),B=c(1,1)) %>%
summarise(A = sum(A),B = sum(B), D=sum(A)-sum(B))
yields D=0
data.frame(A=c(2,2),B=c(1,1)) %>%
summarise(A = sum(A),B = sum(B), D=sum(A-B) )
yields in D=2
data.frame(A=c(2,2),B=c(1,1)) %>%
summarise(sum_A = sum(A),sum_B = sum(B), D=sum(A)-sum(B))
yields in D=2.
I can't seem to come up with an explanation to how the D=0 can be a result of such an operation. How can D=0 be a sensible result?
It is a bug, see https://github.com/tidyverse/dplyr/issues/3233. It is fixed in 0.7.4.9001.

R data table select rows to update column [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 am trying to update a column for a selection of rows based on the value of the same column. this column contains characters from '1','2',....'10','11'. what I want to do is to combine 11 categories into 3. So my code look like
library(data.table)
DT <- data.table(col = as.character(1:11))
DT[col %in% c('1','2','3'), col := '3']
DT[col %in% c('4','5','6','7','8'), col := '2']
DT[col %in% c('9','10','11'), col := '1']
weirdly, the last line doesn't work. the '10' and '11' are not updated. when I change 'c' to list (below code), it seems to work. but i don't know why this is the case.
DT[col %in% list('9','10','11'), col := '1']
Any help will be much appreciated.

How to print out an odd integers 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 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

Resources