user3122022
user3122022

Reputation: 85

List the first occurence in a sample

I have a large dataset (dt) that looks like this:

No  Day1    Day2    Day3    Day4    Day5    Day6    Day7    Day8    Day9    Day10
x   0       0       0       0       0       0       0       1       0       0 
y   0       0       6       2       0       1       0       0       0       0
z   0       0       0       0       0       0       0       0       0       0
a   0       0       2       4       1       1       0       0       0       0

I'd like R to create a one column vector of, for every row, which day (even as a column index is fine) a number other than zero is first encountered (some rows have only zeros). My somewhat clunky code (below) seems to work when I do it iteratively (line by line, using browser) but not when I run it as loops. I get: "Error in if (dt[i, j] > 0.5) { : argument is of length zero" Can someone please tell me what I am doing wrong? Thanks James

days<-c()
for (i in 1:length(rownames(dt))) {
    for (j in 2:11) {

if (dt[i,j]>0.5) {
  x<-j
  break
}
  else {
  next
}
days<-rbind(days,x)
days
}
days
}

Upvotes: 1

Views: 59

Answers (1)

Roland
Roland

Reputation: 132706

Like this?

DF <- read.table(text="No  Day1    Day2    Day3    Day4    Day5    Day6    Day7    Day8    Day9    Day10
x   0       0       0       0       0       0       0       1       0       0 
y   0       0       6       2       0       1       0       0       0       0
z   0       0       0       0       0       0       0       0       0       0
a   0       0       2       4       1       1       0       0       0       0", header=TRUE)

cbind.data.frame(No=DF[,1],
                 first=apply(DF[,-1],1, function(x) which(x!=0)[1]))
#   No first
# 1  x     8
# 2  y     3
# 3  z    NA
# 4  a     3

Or alternatively:

library(reshape2)
DF <- melt(DF)

library(plyr)
ddply(DF, .(No), summarize, first=variable[(value!=0)][1])
#   No first
# 1  a  Day3
# 2  x  Day8
# 3  y  Day3
# 4  z  <NA>

Upvotes: 1

Related Questions