extracting something from a dataframe which contain lists with label information - r

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")

Related

Combine lapply and gsub to replace a list of values for another list of values

I am currently looking for a way to simplify searching through a column within a dataframe for a vector of values and replacing each of of those values with another value (also contained within a separate vector). I can run a for loop for this, but it must be possible within the apply family, I'm just not seeing it yet. Very new to using the apply family and could use help.
So far, I've been able to have it replace all instances of the first value in my vector with the new first value in the new vector, it just isn't iterating past the first level. I hope this makes sense. Here is the code I have:
#standardize tank location
old_tank_list <- c("7.C.4","7.C.5","7.C.6","7.C.7","7.C.8","7.C.9","7.C.10","7.C.11")
new_tank_list <- c("7.B.3-4","7.C.3-4","7.C.1-2","7.C.5-6","7.C.7-8","7.C.9-10","7.E.9-10","7.C.11-12")
sapply(df_growth$Tank,function(y) gsub(old_tank_list,std_tank_list,y))
Tank is the name of the column I am trying to replace all of these values within. I haven't assigned it back yet, because I want to test the functionality first. Thanks for any help you can offer.
Hopefully, this image will help. The photo on the left is the column before my function is applied. The column on the right is after. Basically, I just want to batch change text values.
Before and After
library(dplyr)
df %>%
mutate(Tank = recode(Tank, !!!setNames(new_tank_list, old_tank_list)))

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())

Naming data frames in lists using a sequence

I have a rather simple question. So I have a list, and I want to name the data frames in the list according to a sequence. Right now I have a sequence that increase according to one letter per list (explained below):
nm1 <- paste0("Results_Comparison_",LETTERS[seq_along(Model_comparisons)])
This creates "Results_Comparison_A", "Results_Comparison_B", "Results_Comparison_C", "Results_Comparison_D", etc. What I want is it for it to be a number instead of a letter. (i.e. Results_Comparison_1, Results_Comparison_2, Results_Comparison_3, etc.) Does anyone know how I could change this? If extra information is needed let me know!
This should work paste0("Results_Comparison_",seq_along(Model_comparisons))

How to dynamicly create variables inside a list of dataframes in 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

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?

Resources