R basics and questions [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 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)

Related

In my CSV file, it shows I have 1 column when I actually have 15 columns [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 2 years ago.
Improve this question
> df <- read.csv("DATA ONLY.csv", header = TRUE, sep = ";")
> dim(df)
[1] 439 1
This is the code I use and this is the CSV
https://docs.google.com/spreadsheets/d/1SOqDKXZ7BAMW5LdqBcBIvQE9_PnFcNIHDfYDty3cTto/edit?usp=sharing
I am 99% sure that you have defined the field separator wrong. data.table::fread is really good at sniffing the correct format of csv's, and I quite often use fread even if I just convert the resulting data.table back to vanilla data frame, i.e.
library(data.table)
df <- fread("DATA ONLY.csv")
as.data.frame(df) -> df

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)

Survival analysis [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 years ago.
Improve this question
I get the following error when I run the code below
Error in as.numeric(time) : cannot coerce type 'closure' to vector of type 'double'
Code:
mod <- survreg((Surv(as.numeric(time), event=status)) ~ prison+dose+clinic,
data = meth,
dist = "lognormal")
Hope someone can help with this.
Sandeep
This should solve your problem:
mod <- survreg((Surv(as.numeric(Time), event=Status)) ~ Prison+Dose+Clinic,
data = meth,
dist = "lognormal")
as you reported to me that:
names(meth)
# [1] "ID" "Clinic" "Status" "Time" "Prison" "Dose"
Note, it is Time, not time; Also, it is Status, not status. In fact, all variables names start with a capital letter!

Composing two functions in R gives different result than a new function enclosing both [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 7 years ago.
Improve this question
I tried to look at the logistic map
logMap <- function(x){
r <- 0.5
4*r*x*(1-x)}
and its compositions such as:
logMap2_a <- function(x){
logMap(logMap(x))}
Of course, you can also directly write a new function that composes logMap with itself:
logMap2 <- function(x){
r <- 0.5
16*r*r*x*(1-x)*(1-16*r*r*x*(1-x))}
Now, plotting the two things does not show the same curve:
curve(logMap2, from=0, to=1)
curve(logMap2_a, from=0, to=1)
The first figure shows the result of logMap2, the second that of logMap2_a.
My first idea was that the problem arises due to the precision of returned data, but I cannot imagine that this is a problem with this reasonably large numbers. Any idea what is going on?
logMap2 <- function(x){
r <- 0.5
16*r*r*x*(1-x)*(1-4*r*x*(1-x))}

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