I am trying to create several data names from a for, but the parameter of the for does not recognize me
c.n_vars<-ncol(DATA)
for (i in 3:c.n_vars)
{
datasets[i] <- ts(DATA[,i],start = c(2009,1),frequency = 12)
}
the idea is to create
datasets_1
datasets_2
datasets_3....
is posible?
In R we try not to create lots of similarly named objects. They are difficult to work with and will cause headaches for you later on. Instead we put related objects in lists:
c.n_vars <- ncol(DATA)
datasets <- vector(mode = "list",length = c.n_vars - 2)
for (i in 3:c.n_vars){
datasets[[i]] <- ts(DATA[,i],start = c(2009,1),frequency = 12)
}
If you want the list items to have names you can name them:
names(datasets) <- paste0("dataset_",1:length(dataset))
Related
i'm performing Anova testing for my current datasets that has multiple columns which i am trying to loop to make things easier but it seems to me that i am always facing the same error called "variable lengths differ"
here is my code for the loop:
for(i in 5:125){
WL<- colnames(NB[i])
model <- lm(WL ~ Treatment , data = NB)
if(!exists("aovNB")){
aovNB<-anova(model)
}
if(exists("aovNB")){
aovNB <- rbind(aovNB,anova(model))
}
}
and i'm wondering if it is possible that way to store the column names into WL variable which i can use to read the multiple columns i have.
thanks if anyone could solve it. i'm using base R.
Use reformulate/as.formula to create formula from strings. Also instead of rbinding the datasets in a loop store them in a list.
cols <- colnames(NB)[5:125]
result <- vector('list', length(cols))
for(i in seq_along(cols)){
model <- lm(reformulate('Treatment', cols[i]) , data = NB)
result[[i]] <- anova(model)
}
If needed you can combine them using result <- do.call(rbind, result)
We may do this with paste
cols <- colnames(NB)[5:125]
result <- vector('list', length(cols))
for(i in seq_along(cols)) {
result[[i]] <- anova(lm(as.formula(paste(cols[i], '~ Treatment')), data = NB))
}
I am trying to create different subsets out of a table and with each iteration I want to shift one column upwards. So far I realized this with this code but undynamically:
subset_cor_lag00 <- subset(data_24h, select = c(price_return, sentiment_return, tweet_return))
korr_tab_lag00 <- cor(subset_cor_lag00)
subset_cor_lag01 <- transform(subset_cor_lag00, price_return = lead(price_return))
subset_cor_lag01 <- na.omit(subset_cor_lag01)
korr_tab_lag01 <- cor(subset_cor_lag01)
But now I tried to do this dynamically but I got stuck with it. So maybe someone has a hint. I really would appreciate it. I tried this
for(i in 1:5) {
paste0("subset_cor_lag0", i) <- transform(paste0("subset_cor_lag0", i-1), price_return = lead(price_return))
paste0("subset_cor_lag0", i) <- na.omit(paste0("subset_cor_lag0", i))
paste0("korr_tab_lag0", i) <- cor(paste0("subset_cor_lag0", i))
}
You can use assign for this, but usually having sequentially named variables isn't nice to work with. The better way is to use a list:
subset_cor_lag = list(subset(data_24h, select = c(price_return, sentiment_return, tweet_return)))
for(i in 2:6) {
temp = transform(subset_cor_lag[[i - 1]], price_return = lead(price_return))
subset_cor_lag[[i]] = na.omit(temp)
}
korr_tab = lapply(subset_cor_lag, cor)
## add names, if desired:
name_vec = paste0("lag", 0:5)
names(subset_cor_lag) = name_vec
names(korr_tab) = name_vec
You can then access, e.g., subset_cor_lag[["lag2"]] or subset_cor_lag[[3]], which is easy to do programmatically in a loop or with lapply.
See my answer at How to make a list of data frames? for more discussion and examples.
My objective is to create a number of time-series subsets from a list of variables. I wrote this with a for-loop. However, I'm looking for more elegant ideas on how to do with an existir R function, that doesn't require a loop.
All ideas and intros to new functions in R are much appreciated.
A reproducible example of the code:
russell_sim <- arima.sim(model=list(ar=c(.9,-.2)),n=449)
russell_sim <- ts(russell_sim, start = c(1980,1), end = c(2017,5) ,frequency = 12)
pmi_sim <- arima.sim(model=list(ar=c(.9,-.2)),n=449)
pmi_sim <- ts(russell_sim, start = c(1980,1), end = c(2017,5) ,frequency = 12)
big_list<- list(russell = russell_sim, pmi= pmi_sim)
for (i in 1: length(big_list)) {
assign(paste(names(x = big_list)[i], "_before08", sep = ""), window(big_list[[i]], start=c(1981,1), end=c(2007, 12)) )
}
Thank you.
You can make use of the handy list2env function but you will need to edit the list first to get your desired output:
# New List to edit
big_list_before08 <- big_list
# change your observations
big_list_before08 <- lapply(big_list_before08, function(x) window(x, start = c(1981,1),
end = c(2007,12)))
# change the individual list element names
names(big_list_before08) <- paste0(names(big_list),"_before08")
# save to the global environment
list2env(big_list_before08, envir = .GlobalEnv)
Let me know if you have any questions!
I have hardcoded this:
s79t5 <- read.csv("filename1.csv", header = TRUE)
s81t2 <- read.csv("filename2.csv", header = TRUE)
etc.
subsets79t5 <- subset(s79t5, Tags!='')
subsets81t2 <- subset(s81t2, Tags!='')
...
subsets100t5 <- subset(s100t5, Tags!='')
now i need to softcode it. i am almost there:
sessions <- c('s79t5', 's81t2', 's88t2', 's90t3', 's96t3', 's98t4', 's100t5')
for (i in 1:length(sessions)) {
jFileName <- c(as.character(sessions[i]))
j <- data.frame(jFileName)
subset <- subset(j, j$Tags!='')
assign(paste("subset", jFileName, sep = ""), data.frame(subset))
}
Just throwing an answer here to close this question. Discussion was in the comments.
You need the get function in your line: j <- data.frame(jFileName)
It should be: j <- as.data.frame(get(jFileName))
The get function looks in your existing objects for the string character you gave it (in this case, jFileName) and returns that object. I then make sure it is a data frame with as.data.frame.
Previously you were essentially telling R to make a data frame out of a character string. With get you are now referencing your actual dataset.
I want to create an empty list so I can replace its elements with other lists.
For example
simulations = 10
seeds = sample(10000:99999, simulations, replace=F)
test_function <- function(seedX){
lista = list(seedX=seedX,
dataframe=data.frame(x=runif(10, -5.0, 5.0),y=rnorm(10,mean = 0,sd = 1)))
return(lista)
}
results <- vector("list", simulations)
results[1] = test_function(seedX = seeds[1])
I get the following error:
Warning message:
In results[1] = test_function(seedX = seeds[1]) :
number of items to replace is not a multiple of replacement length
What am I doing wrong?
Thanks!
Just change
results[1] = test_function(seedX = seeds[1])
to
results[[1]] <- test_function(seedX = seeds[1])
The important change is the [...] element indexing operator to the [[...]] list component indexing operator, as you need to assign the list component to the new list. See https://stat.ethz.ch/R-manual/R-devel/library/base/html/Extract.html.
(You should also use the <- assignment operator instead of =, mainly to follow the R convention, but also because = means something different (and therefore can't be used for assignments) in other contexts, such as named parameter specifications in function calls, so using <- allows for more consistency.)