I have created a basic list, and inside this list called lista (not big fantasy I know) there are 10 small dataframes.
Each one of this dataframes is called "numberone","numbertwo",...,"numberten".
When I accede this list I can't see their names.
but the output I can see in the workspace (Rstudio) is this
This below is the code and my tries:
#creating multiple dataframes and a list and then give a title to this dataframes inside the list.
lista = list()
names = c("numberone","numbertwo","numberthree","numberfour","numberfive","numbersix","numberseven","numbereight","numbernine","numberten")
for (i in 1:10) {
x = rnorm(10)
df = data.frame(x)
assign(names[i],df)
lista[[i]] = df
}
#trying to change manually the names of the dataframes inside the "lista" list
names(lista[1]) = "number one"
print(names(lista[1])) #this gives no results
#trying using dput
output = dput(lista[1])
##trying put manually the name in front of the dput output to rename the first dataframe inside lista..
list('numberone'= structure(list(x = c(0.750704535096297, 1.16925878942967,
0.806475114411396, 1.00973486249489, -0.301553383694518, 0.546485320708262,
1.03645444095639, 0.247820396853631, -1.64294545886444, -0.216784798035195
)), class = "data.frame", row.names = c(NA, -10L)))
#this seems to have renamed the first dataframe but, it's not working anyway
lista$numberone
print(names(lista[1])) #still no results
I've tried almost everything I could, but I can't give this single dataframes their names inside the list.
How can i name these dataframes?
Thank You
Try to do names(list)
Here an example using empty lists
list_test = vector("list",4)
names(list_test) = c("A","B","C","D")
list_test
$A
NULL
$B
NULL
$C
NULL
$D
NULL
With your example, I did:
names(lista) <- names
and I get:
names(lista)
[1] "numberone" "numbertwo" "numberthree" "numberfour" "numberfive" "numbersix" "numberseven"
[8] "numbereight" "numbernine" "numberten"
I think you might be looking to use double brackets (e.g.[[1]]) to reference elements in your list. Using your example code, this will work:
names(lista[[1]]) = "number one"
print(names(lista[[1]])) #first element is now called "number one"
You can also use a setNames() function within a Map() function to rename each column for your list of dataframes.
lista <-Map(setNames, lista , names)
lista # each column is now assigned a name from your vector called names
To keep your code clean as possible, it is best to avoid naming objects with the same names as functions. (Your example code uses a vector called "names" but also uses names() function.)
Related
Suppose I have
label <- 'My val'
and I try to create the list
Output <- list(
label = pi
)
I get that the name of the first (only) object in the list is "label" but I want "My val".
I understand I can do
names(Output) <- label
But the list is quite long and I'd rather name it in the list function.
No, you can't reference variables for names when using the list() function to create a list. It will just interpret any variable name as name for the entry. But after constructing the list, you can change the names:
label <- 'My val'
Output <- list(pi)
names(Output)=label
Maybe this could help you, but it would've been much better if you could share a more detailed sample as I thought there might be more variable names involved:
label <- 'My val'
Output <- list(
label = pi
)
Output |>
setNames(label)
$`My val`
[1] 3.141593
Another option is lst from dplyr
library(dplyr)
lst(!!label := pi)
#$`My val`
#[1] 3.141593
I have a list with several elements in it. I want to convert these elements to separated dataframes. However, I dont know how to retain column names. Please see the code below.
for (i in 1: length(mylist)) {
assign(paste("df_",names(mylist[i]),sep = ""), as.data.frame(data.frame(mylist[i]),
col.names = names(dfi)))
}
names(df_april)
> names(df_april)
[1] "april.X" "april.Latitude" "april.month" "april.count1" "april.AOT40_1" "april.count_full"
[7] "april.AOT40_2" "april.State.Code" "april.County.Code" "april.Longitude" "april.Datum" "april.Parameter.Name"
[13] "april.State.Name" "april.County.Name" "april.year" "april.method"
As you can see above, I tried to use as.data.frame with defined col.names = names(dfi) to get rid of "april" in those column names. But it did not work.
Any idea?
In the OP's code, change the [ to [[ as the first is still a list of length 1 and have a list name as well, while the second [[ extracts the list element. So, naturally, when we do the as.data.frame, the list element names also gets appended while flattening that element
for (i in seq_along(mylist)) {
assign(paste("df_",names(mylist[[i]]),sep = ""), mylist[[i]],
col.names = names(dfi)))
}
names(df_april)
NOTE: It is better not to create mutiple objects in the global env.
I want to create a dataframe with 3 columns.
#First column
name_list = c("ABC_D1", "ABC_D2", "ABC_D3",
"ABC_E1", "ABC_E2", "ABC_E3",
"ABC_F1", "ABC_F2", "ABC_F3")
df1 = data.frame(C1 = name_list)
These names in column 1 are a bunch of named results of the cor.test function. The second column should consist of the correlation coefficents I get by writing ABC_D1$estimate, ABC_D2$estimate.
My problem is now that I dont want to add the $estimate manually to every single name of the first column. I tried this:
df1$C2 = paste0(df1$C1, '$estimate')
But this doesnt work, it only gives me this back:
"ABC_D1$estimate", "ABC_D2$estimate", "ABC_D3$estimate",
"ABC_E1$estimate", "ABC_E2$estimate", "ABC_E3$estimate",
"ABC_F1$estimate", "ABC_F2$estimate", "ABC_F3$estimate")
class(df1$C2)
[1] "character
How can I get the numeric result for ABC_D1$estimate in my dataframe? How can I convert these characters into Named num? The 3rd column should constist of the results of $p.value.
As pointed out by #DSGym there are several problems, including the it is not very convenient to have a list of character names, and it would be better to have a list of object instead.
Anyway, I think you can get where you want using:
estimates <- lapply(name_list, function(dat) {
dat_l <- get(dat)
dat_l[["estimate"]]
}
)
cbind(name_list, estimates)
This is not really advisable but given those premises...
Ok I think now i know what you need.
eval(parse(text = paste0("ABC_D1", '$estimate')))
You connect the two strings and use the functions parse and eval the get your results.
This it how to do it for your whole data.frame:
name_list = c("ABC_D1", "ABC_D2", "ABC_D3",
"ABC_E1", "ABC_E2", "ABC_E3",
"ABC_F1", "ABC_F2", "ABC_F3")
df1 = data.frame(C1 = name_list)
df1$C2 <- map_dbl(paste0(df1$C1, '$estimate'), function(x) eval(parse(text = x)))
Let's say one of the columns in my dataframe refers to the name of a city. The city names are expressed as "longformA", "longformB", and I'd like to replace them all with "shrtfrmA", "shrtfrmB". Each "longform" name has an associated "shrtfrm" name with which it should be replaced.
I've got a solution involving a named list and purrr bouncing around in my head, but I can't quite conceptualize it. The named list would have this structure:
city_names_short <- list("ANA" = "Anaheim", "BOS" = "Boston")
And so on, and so forth.
example_df$city[example_df$city == "Anaheim"] <- "ANA"
example_df$city[example_df$city == "Boston"] <- "BOS"
I could of course replace them one by one, as per the above, but I'd like to be a little more elegant.
Any and all advice is greatly appreciated!
I suggest unlisting your list to a named vector and then using match to create the shortform names:
city_names_short <- unlist(city_names_short)
df$shortname <- names(city_names_short)[match(df$city, city_names_short)]
Method 1
You can loop over your city column using sapply:
df$city <- sapply(df$city, function(city) {
names(city_names_short)[city_names_short == city]
})
The function in sapply finds the name (i.e. the shortened city name) of the list item that matches each city name.
Method 2
You can create a map by inverting the city_names_short list:
city_map <- names(city_names_short)
names(city_map) <- city_names_short
df$city <- city_map[df$city]
There is a function setNames in base R:
map = setNames(c("ANA","BOS"),c("Anaheim","Boston"))
df$city_short = map[df$city_long]
I have number of results to pass back to a calling procedure
I'd like to pass back a named list where each result is numbered.
# the following works
# result is a valid result
results = list( "1" = result)
When I do the following I end up with results$resultCounter instead of results$'1'
resultCounter = 1
results = list( resultCounter = result)
How do you pass in the contents of a variable to be the name of an element within a list?
One option would be to use setNames
results <- setNames(result, resultCounter)
data
result <- list(1:5, 6:10)
resultCounter <- 1:2