Extract Name of Data.Frame in R as Character

Extract name of data.frame in R as character

a <- data.frame()
deparse(substitute(a))
[1] "a"

This is also how plot knows what to put on the axes

How to convert the name of a dataframe to a string in R?

The only way I know to work this way directly on the dataframes in a list would be to attach a comment that holds the name, which you can then use to carry its name inside the loop:

df1 <- data.frame(var1=rnorm(10), var2=rnorm(10))
df2 <- data.frame(var1=rnorm(10), var2=rnorm(10))
comment(df1) <- "df1"
comment(df2) <- "df2"

for ( dataFrame in list(df1,df2) ) {
dFnm <- comment(dataFrame)
pdf(file=paste( dFnm, "_var1_vs_var2.pdf", sep="" ))
plot( dataFrame[["var1"]], dataFrame[["var2"]] )
dev.off();
}

(You do lose the names of objects when they get passed as the loop variables. If you do deparse(substitute()) inside that loop, you get "dataFrame" rather than the original names.) The other way would be to use names of the dataframes, but then you will need to use get or do.call, which might get a bit messier. This way seems fairly straightforward.

How to extract the name of a column from a data frame to be used in the loop?

The df[[i]] is extracting the column as a vector and there are no colnames. We can either use df[i] or the correct option is colnames(df)[i]

for (i in 2:3){
assign(paste0("Eval.", colnames(df)[i]), tapply(df[,i], df$Group, summary))
}

-output

Eval.Num.Hats
#$a
# Min. 1st Qu. Median Mean 3rd Qu. Max.
# 6 6 6 6 6 6

#$b
# Min. 1st Qu. Median Mean 3rd Qu. Max.
# 4.00 5.25 6.50 6.50 7.75 9.00

#$c
# Min. 1st Qu. Median Mean 3rd Qu. Max.
# 10.00 10.25 10.50 10.50 10.75 11.00

Eval.Num.Balls
#$a
# Min. 1st Qu. Median Mean 3rd Qu. Max.
# 1 1 1 1 1 1

#$b
# Min. 1st Qu. Median Mean 3rd Qu. Max.
# 3 3 3 3 3 3

#$c
# Min. 1st Qu. Median Mean 3rd Qu. Max.
# 5.00 5.25 5.50 5.50 5.75 6.00

R Extract first two characters from a column in a dataframe

For the last option, it should be uppercase characters ([A-Z]) instead of lowercase ([a-z]) as the input 'sr' column shows only uppercase. In addition, str_extract_all is used when there are multiple occurrences of the pattern and it returns a list (simplify = FALSE by default). Here, the example showed a single occurence, thus str_extract would be more useful as it returns a vector

library(dplyr)
library(stringr)
df %>%
mutate(permit_type = str_extract(sr, "\\b[A-Z]{2}"))
# A tibble: 9 × 5
date_received date_approved sr permit permit_type
<chr> <chr> <chr> <chr> <chr>
1 "11/30/2021 " 11/30/2021 AP-21-080 "AP1766856 Classroom C" AP
2 "11/30/2021 " 11/30/2021 SP-21-081 "AP1766858 Classroom A" SP
3 "11/30/2021 " 11/30/2021 AP-21-082 "AP1766862 Landscape Area" AP
4 "11/30/2021 " 11/30/2021 SP-21-083 "AP1766864 Classroom B" SP
5 "11/30/2021 " 11/30/2021 MP-21-084 "AO1766867" MP
6 "11/17/2021 " 11/17/2021 AP-21-085 "06-SE-2420566" AP
7 "12/3/2021 " 12/3/2021 AP-21-086 "06-E-2425187" AP
8 "12/3/2021 " 12/3/2021 MP-21-087 "" MP
9 "12/13/2021 " 12/3/2021 SP-21-088 "06-SM-2424110" SP

With str_split_fixed directly applying on the data, we can wrap the call within {}

df%>% 
{str_split_fixed(.$sr, "-", 2)[,1]}
[1] "AP" "SP" "AP" "SP" "MP" "AP" "AP" "MP" "SP"

Similar issue in the second case

df%>% 
{str_extract(.$sr, "^.{2}")}
[1] "AP" "SP" "AP" "SP" "MP" "AP" "AP" "MP" "SP"

Extracting the names of a list of data.frames along with values in the data.frame in R

We can use the [[ to do this without anonymous function

v1 <- sapply(j, `[[`, 'one.short')
v1
# AAA BBB CCC
# 0.6 NA 0.4

If we want to set the names of values that NA

names(v1)[is.na(v1)] <- "NULL"
v1
# AAA NULL CCC
# 0.6 NA 0.4

Note that NULLwould need to be in a list because it gets dropped in a vector

c('a', 'b', NULL)
#[1] "a" "b"

list('a', 'b', NULL)
#[[1]]
#[1] "a"

#[[2]]
#[1] "b"

#[[3]]
#NULL

R get data.frame name in the function

deparse(substitute())
returns you the name of a variable, you can call it like this in your function:

DF <- data.frame(c(1,2)); print(paste("DF", deparse(substitute(DF)),sep="="))

How can I extract numbers from dataframe names in a list in R?

Using lapply, we can add new Date column to each data frame in the list, using gsub to obtain the date from the name of each list element.

lst_names <- names(list.data)
list.data <- lapply(lst_names, function(x) {
list.data[[x]]$Date <- gsub("^.*_|[A-Za-z]*\\.\\w+$", "", x)
return(list.data[[x]])
})
names(list.data) <- lst_names


Related Topics



Leave a reply



Submit