R: Adding qualitative data using a text() argument to a for loop: unknown index type 'list' error message

I have sample data which is stored in a data.frame xy and which is plotted in for loop like in the code below. This works all correctly.

xy <- data.frame(NAME=c("NAME1","NAME1","NAME1","NAME2","NAME2","NAME2","NAME3","NAME3"),ID=c(87,87,87,199,199,199,233,233), X_START_YEAR=c(1950,1988,1994,1899,1909,1924,1945,1948),Y_START_VALUE=c(75,25,-90,-8,-55,-10,-9,12),X_END_YEAR=c(1985,1994,1999,1904,1924,1987,1946,1949), Y_END_VALUE=c(20,50,-15,-70,-80,-100,24,59))

1 NAME1  87         1950            75       1985          20
2 NAME1  87         1988            25       1994          50
3 NAME1  87         1994           -90       1999         -15
4 NAME2 199         1899            -8       1904         -70
5 NAME2 199         1909           -55       1924         -80
6 NAME2 199         1924           -10       1987        -100
7 NAME3 233         1945            -9       1946          24
8 NAME3 233         1948            12       1949          59

Now I am trying to incorporate qualitative data which is stored in a data.frame data_qual and I am trying to include this in a second for loop (as in the code below the part that is commented out). But I am running into the error: error in ind[i]: unknown index type 'list'

data_qual <- data.frame(NAME=c("NAME2","NAME3"),ID=c(199,233),X_START_YEAR=c(1986,1905), Y_START_VALUE=c("-X","ST"),X_END_YEAR=c(1987,1907),Y_END_VALUE=c("-X","ST"))

1 NAME2 199         1986            -X       1987          -X
2 NAME3 233         1905            ST       1907          ST

What I am trying to do is to place the qualitative values using a text() argument to place it at its X_END_YEAR position and slightly above the x axis.

What could be wrong about my approach so that I get an error message as indicated above? Does anyone have an idea how I could fix it? Any help is kindly appreciated. The code with the sample data should work when you exclude the second for loop (the part that is commented out) but like this doesn't include the qualitative data.

# split xy by group as defined by ID
ind <- split(xy,xy$ID)

for (i in ind){
  xx = unlist(i[,grep('X_',colnames(i))])
  yy = unlist(i[,grep('Y_',colnames(i))])    
  fname <- paste0(i[1, 'ID'], '.png')
  png(fname, width=1679, height=1165, res=150)
  if(any(xx < 1946)) {my_x_lim <- c(min(xx), 2014)} else {my_x_lim <- c(1946, 2014)} 
       xlab="Time [Years]",
       ylab="Value [m]",
       pch=21, bg='white',
       xlim = my_x_lim,font.lab=2, cex.lab=1.2, cex.axis=1.1)
  axis(1, at = seq(1000, 2050, 5), cex.axis=1, labels=FALSE, tcl=-0.3)
  axis(2, at = seq(-100000, 100000, 500), cex.axis=1, labels=FALSE, tcl=-0.3)
  abline(h=0, col = "gray60")
  i <- i[,-1]
  points(xx, yy, pch=21, bg='white', cex=0.7)
  title(sub=i$warn, adj=0.035, line=-1.5, font=3, cex=1)

  if (i$ID[1] %in% data_qual%ID){
    rel_data_qual <- data_qual[data_qual$ID %in% i$ID,]
    text(x = rel_data_qual$X_END_YEAR,
         y = min(i$Y_END_VALUE + 3),
         labels = rel_data_qual$Y_END_VALUE)  


You cannot use i to index, because i is a data.frame. You can continue using i directly, as you did in the outer for. I think you were trying to do something like this:

for(j in 1:length(which(data_qual$ID %in% i$ID))) {
         x = data_qual[data_qual$ID %in% i$ID, 'X_END_YEAR'] [j],
         y = min(i$Y_END_VALUE+3),
    labels = data_qual[data_qual$ID %in% i$ID, 'Y_END_VALUE'] [j]

But even that is adding an unnecessary for loop. You could just say:

rel_data_qual <- data_qual[data_qual$ID %in% i$ID,]
text(x = rel_data_qual$X_END_YEAR,
     y = min(i$Y_END_VALUE + 3),
     labels = rel_data_qual$Y_END_VALUE)

Because text is vectorized, meaning (loosely) that it can accept vectors of arguments, rather than one at a time.

Need Your Help

Subtype Supertype with Oracle Object Type Creation. Limit on the number of subtypes?

oracle object oracle10g subtype supertype

I have run into an issue when creating a object type in Oracle 10g that inherits from a supertype. We currently have many object types that inherit from this supertype and recently the compiler sta...

zend_form ViewScript decorator / passing arguments

php zend-framework zend-form zend-view

I have a form that is extend from Zend_Form. I am placing the form into a ViewScript decorator like this:

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.