R- How to Dynamically Name Data Frames

R- how to dynamically name data frames?

OP is really struggling so instead of a long comment, I'll show him here. Don't care if this gets closed.

The technical (don't do that answer) would be to use assign:

i <- 1
j <- 1
for (f in 1:length(fileList)){
fileName <- fileList[f]
X <-read.xls(fileName)

if(grepl("Drug_Rep", fileName)) {
print("DRUG")
print(fileName)
assign(paste("X_Drug_Rep", i, sep = '_'), X)
i <- i+1
} else {
print("CONTROL")
print(fileName)
assign(paste("X_CONTROL", i, sep = '_'), X)
j <- j+1
}
}

But as we recommended, you should use lists instead. Using a for loop, it would look like this:

X_Drug_Rep <- list()
X_CONTROL <- list()
i <- 1
j <- 1
for (f in 1:length(fileList)){
fileName <- fileList[f]
X <-read.xls(fileName)

if(grepl("Drug_Rep", fileName)) {
print("DRUG")
print(fileName)
X_Drug_Rep[[i]] <- X
i <- i+1
} else {
print("CONTROL")
print(fileName)
X_CONTROL[[j]] <- X
j <- j+1
}
}

Finally, how your code would look like without a for loop:

drug.rep.files <- grep("Drug_Rep", fileList, value = TRUE)
control.files <- grep("Drug_Rep", fileList, value = TRUE, invert = TRUE)

X_Drug_Rep <- lapply(drug.rep.files, read.xls)
X_CONTROL <- lapply(control.files, read.xls)

Much shorter, no?! Again, this creates two lists. For example, instead of X_Drug_Rep_1, you would access the first Drug_Rep item by doing X_Drug_Rep[[1]].

Dynamically create/name a series of data.frames and bind them together

As we have create multiple objects in the global env (not recommended), check those objects in the global env with ls with a regex as pattern

ls(pattern = "^df_months_\\d+$")

It returns a vector of object names that matches the pattern - df_months_ from the start (^) of the string followed by one or more digits (\\d+) till the end ($) of the string

Now, we get the values of the objects. For >=1 objects, use mget which returns a key/value pair as a named list.

mget(ls(pattern = "^df_months_\\d+$"))

Then, we use rbind within do.call to bind the elements of the list

do.call(rbind, mget(ls(pattern = "^df_months_\\d+$")))

Create dynamic R dataframe names in for loop - multiple names in same code line

If you need both data frames ("Data i" and "New Data i") you can use:

for (i in 1:3){
assign(paste("New data",i,sep=""), data.frame(assign(paste("Data",i,sep=""),data.frame(colone=c(1,2,3,4),coltwo=c(5,6,7,8))) %>% mutate(colthree=(colone+coltwo)*i) %>% select(colthree)))
}

If you only want "New Data i" use:

for (i in 1:3){
assign(paste("New data",i,sep=""), data.frame(colone=c(1,2,3,4),coltwo=c(5,6,7,8))) %>% mutate(colthree=(colone+coltwo)*i) %>% select(colthree)
}

R dynamic data frame names in Loop

We can use mget to return the value of the string objects in a list, then loop through the list with lapply get the summary

lapply(mget(paste0("df", seq_len(n))), summary)

If we are using a for loop, make sure that we store the results in an object, preferably a list, use get to return the value of the object, get the summary and store it as list element in the 'out' object

out <- vector('list', n)
for(i in seq_len(n)) {
out[[i]] <- summary(get(paste0("df", i)))
}

NOTE: It is better to have all the data.frame in a list. But, in case we want to update the original objects in the global environment using for loop use assign

for(i in seq_len(n)) {
assign(paste0("df", i), get(paste0("df", i))[-(1:3)])
}

Or this can be done with list2env after using lapply

list2env(lapply(mget(paste0("df", seq_len(n))), function(x) x[-(1:3)]), .GlobalEnv)

data

df1 <- data.frame(col1 = 1:3, col2 = 4:6, col3 = 7:9, col4 = 10:12)
df2 <- data.frame(col1 = 1:15, col2 = 16:30, col3 = 31:45, col4 = 46:60)
n <- 2

Dynamic name dataframe R for loop

paste on the lhs of <- shouldd be replaced by assign if we want to create multiple objects in the global env (not recommended though)

for (i in seq(1,len_TSPAN)){
temp_TSPAN <- TSPANS$V1[i]
print(temp_TSPAN)
assign(as.character(temp_TSPAN), value = data.frame(Lum_A_Q1_means = rep(NA, 67), Lum_A_Q2_means = rep(NA,67),
Lum_A_Q3_means = rep(NA, 67), Lum_A_Q4_means = rep(NA,67),
Lum_B_Q1_means = rep(NA, 67), Lum_B_Q2_means = rep(NA,67),
Lum_B_Q3_means = rep(NA, 67), Lum_B_Q4_means = rep(NA,67),
Her_2_Q1_means = rep(NA, 67), Her_2_Q2_means = rep(NA,67),
Her_2_Q3_means = rep(NA, 67), Her_2_Q4_means = rep(NA,67),
Basal_Q1_means = rep(NA, 67), Basal_Q2_means = rep(NA,67),
Basal_Q3_means = rep(NA, 67), Basal_Q4_means = rep(NA,67),
Normal_Q1_means = rep(NA, 67), Normal_Q2_means = rep(NA,67),
Normal_Q3_means = rep(NA, 67), Normal_Q4_means = rep(NA,67))
)
}

It seems that we are creating the same 'data.frame' object in each loop. It may be easier with replicate and store in a list

lst1 <- replicate(len_TSPAN), 
data.frame(Lum_A_Q1_means = rep(NA, 67), Lum_A_Q2_means = rep(NA,67),
Lum_A_Q3_means = rep(NA, 67), Lum_A_Q4_means = rep(NA,67),
Lum_B_Q1_means = rep(NA, 67), Lum_B_Q2_means = rep(NA,67),
Lum_B_Q3_means = rep(NA, 67), Lum_B_Q4_means = rep(NA,67),
Her_2_Q1_means = rep(NA, 67), Her_2_Q2_means = rep(NA,67),
Her_2_Q3_means = rep(NA, 67), Her_2_Q4_means = rep(NA,67),
Basal_Q1_means = rep(NA, 67), Basal_Q2_means = rep(NA,67),
Basal_Q3_means = rep(NA, 67), Basal_Q4_means = rep(NA,67),
Normal_Q1_means = rep(NA, 67), Normal_Q2_means = rep(NA,67),
Normal_Q3_means = rep(NA, 67), Normal_Q4_means = rep(NA,67)), simplify = FALSE)

How to assign and dynamically change the name of a dataframe in R in a loop

With the suggestion of Waldi I was able to solve it

for (i in 1:nrow(data_s_y_revenue)){

name_y <- data_s_y_revenue$year[i]

name_y1 <- paste("data_y_y",name_y, sep="_")
data_y_y <-filter(data_y, year==name_y)

assign(name_y1, data_y_y)

}

Data frame creation with dynamically assigned variable names

Based on the OP comment, the solution would be:

#Code
assign(y, x)

For the other issue you can try:

#Code2
df <- data.frame(assign(y, x))
names(df)[1] <- y

Output:

df
z
1 -0.5611014
2 -2.2370362
3 0.9037152
4 -1.1543826
5 0.4997336
6 -0.4726948
7 -0.6566381
8 1.0173725
9 -0.5230326
10 -0.9362808

How can i pass dynamic column name from one data frame to my conditions in R?

This may be what you wanted to do?

library(dplyr)


df1 %>%
anti_join(df2, by = df3$ColumnName)

# Name Data Identifier
# 1 Punit 13q q

Dynamically Name R Data Frame in a For Loop

R handles this easily. Here is an approach, but it may need modification depending on what you want to do with all these random data sets. This builds a list with 100 matrices labeled "data001" to "data100":

library(MASS)
d.cor <- .10
DATA <- replicate(100, mvrnorm(n=20, mu=c(0, 0), Sigma=matrix(c(1,
d.cor, d.cor, 1), ncol=2), empirical=TRUE), simplify=FALSE)
names <- paste0("data", sprintf("%0003d", 1:100))
names(DATA) <- names
head(DATA[["data099"]])
# [,1] [,2]
# [1,] 1.94086111 1.570299681
# [2,] -0.74071651 -0.664948968
# [3,] -1.02952487 -0.704650191
# [4,] 0.85203916 0.698703243
# [5,] -0.08673212 1.668412324
# [6,] 0.88828524 0.001039757
save(DATA, file="AllData.RData")

This code creates a list containing 100 matrices and names each matrix. You can access a particular matrix with the name or number, DATA[["data099")]] or DATA[[99]]. It is saved as "AllData.RData" so that you can retrieve it with load("AllData.RData"). Depending on what you plan to do with this data, a list is probably more flexible than 100 separate files.



Related Topics



Leave a reply



Submit