Don't think of R like a spreadsheet program. It's important to think column-wise when you use R. There is almost never a need to create a new row for anything that contains an "answer".
What I would suggest is to create a set of columns that contain True/False values for negative numbers. Then use subscripting on them.
Here is a tiny example, step by step:
> dput(my.data)
structure(list(Year = c(2000, 2001, 2002), V1 = c(3, -4, -10),
V2 = c(-500, -1000, -1200), V3 = c(1, 2, -3)), class = "data.frame", row.names = c(NA,
-3L))
#> my.data
# Year V1 V2 V3
# 2000 3 -500 1
# 2001 -4 -1000 2
# 2002 -10 -1200 -3
# are any values in V1, V2, or V3 less than 0?
my.data$V1.neg.TF <- my.data$V1.neg.TF[my.data$V1.neg.TF][1] < 0
my.data$V2.neg.TF <- my.data$V2.neg.TF[my.data$V2.neg.TF][1] < 0
my.data$V3.neg.TF <- my.data$V3.neg.TF[my.data$V3.neg.TF][1] < 0
#> my.data
# Year V1 V2 V3 V1.neg.TF V2.neg.TF V3.neg.TF
#1 2000 3 -500 1 FALSE TRUE FALSE
#2 2001 -4 -1000 2 TRUE TRUE FALSE
#3 2002 -10 -1200 -3 TRUE TRUE TRUE
Notice how the True/False values indicate where the negative values appear first.
Now we use subscripting:
my.data$Year[my.data$V1.neg.TF][1]
this says to grab the sequence of Year (my.data$Year
) where there is a negative (my.data$V1.neg.TF
) and then return the first element of that vector ([1]
).
So you could do
c(
my.data$Year[my.data$V1.neg.TF][1],
my.data$Year[my.data$V2.neg.TF][1],
my.data$Year[my.data$V3.neg.TF][1]
)
which would give you 2001 2000 2002
, which is what I think you want.