Ok, so I try to do the most basic operation, but it seems quite complicated with R.
What I would like to achieve is to iterate through a list.
To acces a particular value within this list, I just need to write:
list.data$V10.csv$Value[2]
I would like to replace the V01 by a variable, so that I can use a loop.
Below is what I try so far:
k <- 10
test0 <- paste0("V",k)
test1 <- as.numeric(paste0("V", k))
Which will theoretically allow me to access the same value as before by writing:
list.data$test0.csv$Value[2]
// Or
list.data$test1.csv$Value[2]
And use a loop by only changing the value of k.
But it does not work. How can I do it?
Thanks in advance!
It is better to use [[ for passing object names to extract an element from a list.
test0 <- paste0("V",k, ".csv")
list.data[[test0]]$Value[2]
Related
I am coding this in r and solved this in an alternative way to make the vector to a list and assign value to each of the element of the list, but is there any other direct simple approach?
for(i in 1:5){
paste('var',i,sep='')=i
}
i want output where 1:5 will assign like
var1=1
var2=2
var3=3
var4=4
var5=5
Don’t do this. Use a vector or list instead:
var = 1 : 5
Now you can use var[1] (instead of var1) etc.
Your code doesn’t work because paste creates a character vector, not a variable name.
I have data.frame objects in the list as an output of custom function, and I intend to proceed duplicate removal only first data.frame objects, while others shouldn't not be effected. I tried this in lapply function to control this constrain, but I have subscript error instead. I know this easier to do it in separately, but this is not desired for me. Can anyone point me out how to make this easier in functional programming ? Does anyone knows any useful trick of controlling the constrain on specific objects in the list ?
mini example:
myList <- list(
bar = data.frame(v1=c(12,21,37,21,37), v2=c(14,29,45,29,45)),
cat = data.frame(v1=c(18,42,18,42,81), v2=c(27,46,27,46,114)),
foo = data.frame(v1=c(3,3,33,3,33,91), v2=c(26,26,42,26,42,107))
)
it is easy to do like this:
.first <- unique(myList[[1L]])
res <- c(list(.first), myList[- 1L])
but I need to constrain duplicate removal only effect on the first data.frame, while others doesn't do remove duplicate, I intend to implement this in function more elegant way.
desired output:
myOutput <- list(
bar = data.frame(v1=c(12,21,37),v2=c(14,29,45)),
cat = data.frame(v1=c(18,42,18,42,81), v2=c(27,46,27,46,114)),
foo = data.frame(v1=c(3,3,33,3,33,91), v2=c(26,26,42,26,42,107))
)
If we need to use lapply, then we can loop through the sequence of list, and with if/else modify the list elements
lapply(seq_along(myList), function(i) if(i==1) unique(myList[[i]]) else myList[[i]])
Or else we can assign the modified list element
myList[[1]] <- unique(myList[[1]])
I am implementing k-means in R.
In a loop, I am initiating several vectors that will be used to store values that belong to a particular cluster, as seen here:
for(i in 1:k){
assign(paste("cluster",i,sep=""),vector())
}
I then want to add to a particular "cluster" vector, depending on the value I get for the variable getIndex. So if getIndex is equal to 2 I want to add the variable minimumDistance to the vector called cluster2. This is what I am attempting to do:
minimumDistance <- min(distanceList)
getIndex <- match(minimumDistance,distanceList)
clusterName <- paste("cluster",getIndex,sep="")
name <- c(name, minimumDistance)
But obviously the above code does not work because in order to append to a vector that I'm naming I need to use assign as I do when I instantiate the vectors. But I do not know how to use assign, when using paste, when also appending to a vector.
I cannot use the index such as vector[i] because I don't know what index of that particular vector I want to add to.
I need to use the vector <- c(vector,newItem) format but I do not know how to do this in R. Or if there is any other option I would greatly, greatly appreciate it. If I were using Python I would simply use paste and then use append but I can't do that in R. Thank you in advance for your help!
You can do something like this:
out <- list()
for (i in 1:nclust) {
# assign some data (in this case a list) to a cluster
assign(paste0("N_", i), list(...))
# here I put all the clusters data in a list
# but you could use a similar statement to do further data manipulation
# ie if you've used a common syntax (here "N_" <index>) to refer to your elements
# you can use get to retrieve them using the same syntax
out[[i]] <- get(paste0("N_", i))
}
If you want a more complete code example, this link sounds like a similar problem emclustr::em_clust_mvn
I would like to form a list thanks to a loop.
I have a list of variables called:
var1, var2, ... varN
And I would like to create easily a list of length named listvar with:
unlist(listvar[i])=vari (with i in 1:N)
Is someone inspired ?
The code makes me wonder why the variables var1 … varN exist in the first place: they shouldn’t. Instead, generate the list directly.
That said, you can easily retrieve the value of a variable given by its name using get. This doesn’t even require a loop, you can use R’s vectorised operations.
varnames = paste0('var', 1 : N)
listvar = mget(varnames)
I would like to loop over a string variable. For example:
clist <- c("BMI", "trig", "hdl")
for (i in clist) {
data_FK_i<-subset(data_FK, subset= !is.na(FK) & (!is.na(i)))
}
The "i" should receive a different name from the list.
What am I doing wrong? It's not working? Adding "" doesn't seem to help.
Thank,
Einat
Thanks, the "assign" answer did the work!!!!!!!!!!
I agree with #Thomas. You should use a list. However, let me demonstrate how to modify your code to create multiple objects. You can use the function assign to create objects based on strings.
clist <- c("BMI", "trig", "hdl")
for (i in clist) {
assign(paste0("data_FK_", i), complete.cases(data[c("FK", i)]))
}
Try something like this instead, which will give you a list containing the three subsetted dataframes:
lapply(clist, function(x) data_FK[ !is.na(data_FK$FK) & !is.na(data_FK[,x]) ,])
The problem in your code is that i is a character string, specifically one of the values from clist in each iteration of the for-loop. So, when R reads !is.na(i) you're saying !is.na("BMI"), etc.
Various places on Stack Overflow advise against using subset at all in favor of extraction indices (i.e., [) like in the example code above because subset relies on non-standard evaluation that is confusing and sometimes leads you down bad rabbit holes.
Is this what you want?
You need to give the loop something to store the data into.
Also you need to tell the loop how long you want it to run.
clist <- c("BMI", "trig", "hdl")
#empty vector
data_FK<-c()
#I want a loop and it will 'loop' 3 times (1 to 3), which is the length of my list
for (i in 1:length(clist)) {
#each loop stores the corresponding item from the list into the vector
data_FK<-c(data_FK,clist[i])
}
## or if you want to store the values in a data frame
## there are other ways to create this, but here is a simple solution
data_FK<-data.frame(placer=1:length(clist))
for(i in 1:length(clist)){
data_FK$items[i]<-clist[i]
}
## or maybe you just want to print the names
for (i in 1:length(clist)){
print(clist[i])
}