How to dynamicly create variables inside a list of dataframes in R - r

got a list named x which has 25 elements. Each of which is named after a country. Inside each country i have 10-20 variables and i want to create a new one inside each dataframe which uses one already existing variable (lets name it y) to create a new variable.
closest i have come to solving it is using a for loop but using the [[<- creates newvariables outside the list (which is too much of a hustle to put in again) with "[[" in their name.
also assign doesnt work either due to invalid first argument error (propably due to my effort to dynamically create them)
i guess a nested lapply is the best option but being new to R dont know how that would work

We can loop over the list with lapply and transform to create a new variable
lapply(lst, transform, newVar = y)

I do not really understand your question, could you paste some code and say what you'd like to see. When I have to create variables names dynamically I use trick as the one below:
names(tmp) = paste('y', 1:25, sep='')
If you search help(names) you should get how to change columns and row names specifically. Paste is just a way to concatenate different types of text you want into your variable names

Related

extracting something from a dataframe which contain lists with label information

Sorry if this is basic, I have great difficulty knowing how to extract things out of dataframes, especially when there are lists and other items contained within.
Please see below image of what I see when I click on the dataframe in the global environment. I have a dataframe (lets call it "df") with the variable S1 (it was SPSS file imported using Haven).
[![how it looks in environment][1]][1]
I know df$S1 gets me the values for the variable. But I'd just like to extract the information that is value "labels" and "names" and put them into another dataframe. Is there a way to do it? I'd like to do it for all variables, but if you can start me off just to extract these two from that one variable, I can look at doing it using a loop. Any help greatly appreciated. Many thanks
We can use attr or attributes
attributes(df$S1)
returns a list with the key as the names label, format.spss etc. From the above, just use the $ or [[ for extraction i.e. for the normal list
attributes(df$S1)$label
Based on the updated image, we may need #
attributes(df$S1)#label
If we use attr
attr(df$S1, "label")

Efficient way of extracting names of a large number of variables in R

It could be a very easy question, given that I am very unfamiliar with R. I know normally one can use deparse(substitute(.)) to extract the name of a variable. However, if I have a long list of variables (let's say it's built without names), how can I extract the name of each variable efficiently? I was thinking about using loops, but the deparse(substitute(.)) method would obviously generate the 'general' variable name we used to denote every item.
Sample code:
countries<-
list(austria,belgium,czech,denmark,france,germany,italy,luxemberg,netherlands,poland,swiss)
Suppose I want to get countryNames equals to list("austria","belgium",...,"swiss"), how shall I code? I tried generating the list using countries <- list(countryA = countryA, countryB = countryB, ...), but it was extremely tedious, and in some cases I might only have an unnamed input list from elsewhere.
countries would just have values of each individual objects (austria,belgium etc.). To access the names you need to create a named list while creating countries which can be done like :
countries <- list(austria = austria,belgium = belgium....)
However, if this is very tedious you can use tibble::lst which creates the names automatically without explicitly mentioning them.
countries <- tibble::lst(austria,belgium....)
In both the case you can access the names using names(countries).
If the country objects are the only ones loaded in the global environment, we can do this easily with ls and mget to return a named list of values
countries <- mget(ls())

Remove all R Data objects from Global Environment

I need a function to remove all objects on Data field of Global Environment (the one highlighted below).
I don't know specifically all classes of objects that appears there, however, I would like to remove everything, except for vectors, integers and functions.
Thanks in advance.
The data tab seems to hold anything with more than one dimension.
If you do ls(), you get character strings of the names of all the objects in the global environment. You can represent any of these objects by calling get("object_name"), so you can get the number of dimensions it has by calling length(dim(get("object_name"))). If this value is greater than 1, you know this is one of the objects you want to remove.
Therefore, all you need to do is apply length(dim(get("object_name"))) > 1 to the names of the global objects, as obtained by ls(). You can do this with sapply:
rm(list = ls()[sapply(ls(), function(x) length(dim(get(x))) > 1)])
Use the below code: in place of those variables you want to keep place in quotes see below example.
rm(list=setdiff(ls(), "keep_variable"))
Another option is to change list to grid and click on the variables you don't want and press clean button. That will remove all unwanted variables.

How to remember which variables are in a list

I have a huge list in which I put different variables in order to apply the same function to all of them.
In a next step I want to apply specific functions to specific elements of the list, i.e. all functions used vary from element to element within the list.
How can I do this? My first idea was (see my other question, Reassign variables to elements of list) to split the list into the original variables again. This can be done.
But I was recommended to keep the items in the list instead. My questions is: How can I access each variable quickly by doing that? One idea would be to use the names attribute of the list in the beginning and fill it with a vector of the original variable names. However, by doing that it would be much longer later on to type list["name_x"] than just typing name_x assuming name_x is globally available.
What is the most efficient way to deal with my problem?

Using a list of matrix names

I have 75 matrices that I want to search through. The matrices are named a1r1, a1r2, a1r3, a1r4, a1r5, a2r1,...a15r5, and I have a list with all 75 of those names in it; each matrix has the same number of rows and columns. Inside some nested for loops, I also have a line of code that, for the first matrix looks like this:
total <- (a1r1[row,i]) + (a1r1[row,j]) + (a1r1[row,k])
(i, j, k, and row are all variables that I am looping over.) I would like to automate this line so that the for loops would fully execute using the first matrix in the list, then fully execute using the second matrix and so on. How can I do this?
(I'm an experienced programmer, but new to R, so I'm willing to be told I shouldn't use a list of the matrix names, etc. I realize too that there's probably a better way in R than for loops, but I was hoping for sort of quick and dirty at my current level of R expertise.)
Thanks in advance for the help.
Here The R way to do this :
lapply(ls(pattern='a[0-9]r[0-9]'),
function(nn) {
x <- get(nn)
sum(x[row,c(i,j,k)])
})
ls will give a list of variable having a certain pattern name
You loop through the resulted list using lapply
get will transform the name to a varaible
use multi indexing with the vectorized sum function
It's not bad practice to build automatically lists of names designating your objects. You can build such lists with paste, rep, and sequences as 0:10, etc. Once you have a list of object names (let's call it mylist), the get function applied on it gives the objects themselves.

Resources