crayfishcray
crayfishcray

Reputation: 409

R: Looping through list of dataframes in a vector

I have a dataset where I only want to loop through certain columns in a dataframe one at a time to create a graph. The structure of my dataframe consists of data that I parsed from a larger dataset into a vector containing multiple dataframes.

I want to call one column from one dataframe in the vector. I want to loop on the dataframes to call each column.

See example below:

d1 <- data.frame(y1=c(1,2,3),y2=c(4,5,6))
d2 <- data.frame(y1=c(3,2,1),y2=c(6,5,4))
my.list <- list(d1, d2)

All I have to work with is my.list

How would I do this?

Upvotes: 2

Views: 4539

Answers (4)

Patrick Reed
Patrick Reed

Reputation: 36

I think the simplest answer to your question is to use double brackets.

for (i in 1:length(my.list)) {

print(my.list[[i]]$column)

}

That works assuming all of the columns in your list of data frames have the same names. You could also call the position of the column in the data frame if you wanted.

Yes, lapply can be more elegant, but in some situations a for loop makes more sense.

Upvotes: 0

Whitebeard
Whitebeard

Reputation: 6193

You can use lapply to plot each of the individual data frames in your list. For example,

d1 <- data.frame(y1=c(1,2,3),y2=c(4,5,6),y3=c(7,8,9))
d2 <- data.frame(y1=c(3,2,1),y2=c(6,5,4),y3=c(11,12,13))
mylist <- list(d1, d2)

par(mfrow=c(2,1))

# lapply on a subset of columns
lapply(mylist, function(x) plot(x$y2, x$y3))

enter image description here

Upvotes: 1

yaleeconjournal
yaleeconjournal

Reputation: 329

Here is a solution to your problem using a for loop:

# a toy dataframe
mylist <- list(dat1 = data.frame(A = 1:20, B = LETTERS[1:20]),
           dat2 = data.frame(A = 21:40, B = LETTERS[1:20]),
           dat3 = data.frame(A = 41:60, B = LETTERS[1:20]))
col_names <- c("A") # name of columns I want to get
for (i in 1:length(mylist)){
    # you can do whatever you want with what is returned;
    # here I am just print them out
    print(names(mylist)[i]) # name of the data frame
    print(mylist[[i]][,col_names]) # values in Column A 
}

Upvotes: 0

yaleeconjournal
yaleeconjournal

Reputation: 329

You don't need a for loop to get their data points. You can call the column by their column names.

# a toy dataframe
d <- data.frame(A = 1:20, B = sample(c(FALSE, TRUE), 20, replace = TRUE),
            C = LETTERS[1:20], D = rnorm(20, 0, 1))
col_names <- c("A", "B", "D") # names of columns I want to get
d[,col_names] # returns a dataset with the values of the columns you want

Upvotes: 0

Related Questions