0

I am a relative newcomer to R so I'm probably asking one of the easiest things here. I have 10 columns of the same type of data that I would like to accumulate under one name. The column headings are:

egg_diameter1, egg_diameter2...egg_diameter10

How can I accumulate this under one common name? Say egg.diameter There are also a load of zero values that I would like to eliminate from the study as well. Can anybody include the code for this as well? Thanks!

JAM10100
  • 13
  • 3

1 Answers1

1

If you want the 10 columns to be reduced to 1 column

 dat1 <- data.frame(egg.diameter=unlist(dat, use.names=FALSE))

Or

  dat2 <- setNames(stack(dat)[,1,drop=FALSE], "egg.diameter")

If you want to remove the 0 values

  dat2[dat2[,1]!=0,,drop=FALSE]

data

set.seed(24)
dat <- as.data.frame(matrix(sample(0:40, 10*20,replace=TRUE),
          ncol=10, dimnames=list(NULL, paste("egg", paste0("diameter",1:10)))))
akrun
  • 874,273
  • 37
  • 540
  • 662