Reputation: 185
I am plotting 7 histograms from a a list and want to update the title of the histogram depending on the position of the list.
This is what I got so far but I am getting the following error when executing the script.
Error in item_df[df] : invalid subscript type 'list'
full_df <- read.csv("filename")
item_df <- split(full_df, full_df$PIA_ITEM)
par(mfrow = c(3,3)) # 3 rows and 3 columns
for (df in item_df) {
hist(df[[7]], breaks=9, main = paste("Histogram of", names(item_df[df])))
}
The error points at my attempt of using the looping index to draw the list name position
main = paste("Histogram of", names(item_df[df]))
Upvotes: 1
Views: 11222
Reputation: 39605
As data is not provided, I add an example with dummy data and then a possible sketch for your problem. Based on your code I think you are trying to add as title the value of PIA_ITEM
and plotting the variable in position 7. This dummy example works for iris
:
full_df <- iris
item_df <- split(full_df, full_df$Species)
par(mfrow = c(3,3)) # 3 rows and 3 columns
for (i in 1:length(item_df))
{
hist(item_df[[i]]$Sepal.Length, breaks=9,
main = paste("Histogram of", unique(item_df[[i]]$Species)),
xlab = 'x')
}
And produces:
And the code for your problem should be something like this:
full_df <- read.csv("filename.csv")
item_df <- split(full_df, full_df$PIA_ITEM)
par(mfrow = c(3,3)) # 3 rows and 3 columns
for (i in 1:length(item_df))
{
hist(item_df[[i]][,7], breaks=9,
main = paste("Histogram of", unique(item_df[[i]]$PIA_ITEM)),
xlab = 'x')
}
Upvotes: 2
Reputation: 56189
Adapt this example:
item_df <- split(mtcars[, 1], mtcars$cyl)
par(mfrow = c(1, 3))
for (i in names(item_df)){
hist(item_df[[ i ]], main = paste("Histogram of", i))
}
Upvotes: 0