Subcript out of bounds in R - r

I am using Rstudio 2022.07.1 on Window. I am new to R and trying to sort a porfolio of stocks with the following steps:
I will base on a value of be_me in June of each year to sort my portfolio.
If the value of be_me in June is smaller than the average value of be_me in the year, I will assign the stock as "S" (small). Otherwise, the stock is considered "B" (big).
Here is the code I write, and the error is "Error in sort_size[[x]] : subscript out of bounds":
for (i in seq_along(year)) {
for (j in seq_along(month)) {
if (j == 06) {
average_me <- mean(be_me)
sort_size <- vector('list', length=1)
for (x in seq_along(be_me)) {
if (isTRUE(x<= average_me)==TRUE) {
sort_size[[x]] == "S"
}else{sort_size[[x]] == "B"}
}
}
}
}
lapply(sort_size, print)
Could you please to show me how to fix the error as well as if you could recommend me any better way to do the task.
Thank you very much for your help!

Related

Loop command only consist of last value

I am trying to read.csv a series of file named ("year".csv) from year 2005 to 2007 and writing into database.
However the loop command only consist of "2007L" for element "i" and only read 2007.csv into the dataframe.
Any ideas on how to resolve the issue ?
for(i in c(2005:2007)){
ontime<-read.csv(paste0(i,".csv"), header=TRUE)}
if(i == 2005){
dbWriteTable(conn,"ontimet",ontimet)
} else {
dbWriteTable(conn,"ontimet",ontimet, append= TRUE)
}
}
Something like this might be what you need. I think you just need to assign variables and append in the right place.
You probably need is like this. It produces all three years (or in your case, it would be csv's)
tmp = NULL
for(i in c(2005:2007)){
if(i == 2005){
tmp = append(tmp, i)
} else {
tmp = append(tmp, i)
print(tmp)
}
}

How to implement a function with a sum inside in R?

I am trying to define a function with a for loop and inside a conditional in R studio. Yesterday I was able with the help of another thread to devise this piece of code. The problem is that I want to sum the vector elements ma for any possible x, so that is inside the function l. This is a simpler case which I am trying to solve to adapt the original model. However, I do not know how to proceed.
ma<-rep(0,20)
l <- function(x, ma) {
for(i in seq_along(ma)) {
if(i %% 2 == 1) {
ma[i] <- i + x
} else {
ma[i] <- 0
}
}
return(ma)
}
My problem is that I would like to have the sum of i+x+0+i+x... for any possible x. I mean a function of the kind for any possible x.
Question:
Can someone explain to me how to implement such a function in R?
Thanks in advance!
I am going to update the original function:
Theta_alpha_s<-function(s,alpha,t,Basis){
for (i in seq_along(Basis)){
if(i%% 2==1) {Basis[i]=s*i^{-alpha-0.5}*sqrt(2)*cos(2*pi*i*t)}
else{Basis[i]=s*i^{-alpha-0.5}*sqrt(2)*sin(2*pi*i*t)}
}
return(Basis)
}
If you don't want to change the values in Basis, you can create a new vector in the function (here result) that you will return:
l = function(s,alpha,t,Basis){
is.odd = which(Basis %% 2 == 1)
not.odd = which(Basis %% 2 == 0)
result = rep(NA, length(Basis))
result[is.odd] = s*is.odd^{-alpha-0.5}*sqrt(2)*cos(2*pi*is.odd*t)
result[not.odd] = s*not.odd^{-alpha-0.5}*sqrt(2)*sin(2*pi*not.odd*t)
#return(result)
return(c(sum(result[is.odd]), sum(result[not.odd])))
}

why c() does not working in this recursive function in R?

enter image description here
I know there exists function 'unique' which works similar to what I want to make, but I want to make this function.
I want this function finally returns 'result' which contains unique elements of input vector.
But I don't know why this function's result is totally different from my expect.
Why c which is to combine before result and new unique element is not working.
Please tell me how to fix my code.
Thank you.
I think what you expect might be something like below, where result should be an argument of m_uni:
m_uni <- function(x,result = c()) {
if (class(x)=='numeric'| class(x)=='character') {
if (length(x) <= 1){
return(result)
} else {
if (x[length(x)] %in% result) {
x <- x[-length(x)]
m_uni(x,result)
} else {
result <- c(result,x[length(x)])
x <- x[-length(x)]
m_uni(x,result)
}
}
} else {
return('This function only gets numeric or character vector')
}
}
such that
> m_uni(x)
[1] 0 4 5 -2

if/else statement evaluating only else statement

Disclaimer: This is a question regarding an assignment for a Coursera course.
I'm having trouble coming up with a way to create a new column that differentiates between weekdays and weekends in my data set. I'm using a nested if/else statement within a for loop. The problem is the output makes every row 'weekday'. Does anyone see something glaringly wrong with my code? My end goal is to create a new factor variable that is either "weekend" or "weekday."
df4 <- mutate(df4, day = weekdays(df4$date))
for (i in df4$day) {
if(i %in% c("Saturday",'Sunday')) {
df4$day_type <- 'weekend'
} else {
df4$day_type <- 'weekday'
}
}
I modify a little bit of your code .(see below)
for (i in 1 : dim(df4)[1]) {
if(df4$day[i] %in% c('Saturday','Sunday')) {
df4$day_type[i] <- 'weekend'
} else {
df4$day_type[i] <- 'weekday'
}
}

function to recursively remove outliers

I wrote a function to remove outliers resursively (for any data points 3sd away from its median.
rm.outlier <- function (var) {
has.3sd =1
while (has.3sd>0) {
for (l in var) {
if ( (l-median(var))> 3*sd(var) & !is.na(l)) {
var[var==l] <- NA
}
}
has.3sd <- sum(var > 3*sd(var))
if (has.3sd==0) {
break
}
}
return (var)
}
However, I always got the error message:
Error in if ((l - median(var)) > 3 * sd(var) & !is.na(l)) { :
missing value where TRUE/FALSE needed
I wonder why I got this error message? I spent long time trying to figure it out but couldn't. I appreciate it if anyone can help me with that. Thanks a lot.

Resources