Hi,

I am trying to reshape data from a long to wide format but have a specific
task that I cannot get to output properly.

# SAMPLE DATA;
id <- c(1,2,2,3,3,3)
time <-c(0,0,5, 0, 2, 10)
x <- rnorm(length(id))
long <- data.frame(id,time,x)

# To reshape, I would like to exclude 'id' values that have NO duplicate
(i.e., remove
#  id=1 in this case). My attempts failed because the functions removes the
first value of each id (and I would like to preserve them if length of each
unique id >1), e.g.:

junk <- long[duplicated(long$id),]  # REMOVES TOO MANY ROWS!
junk <-subset(long, seq(id) - match(id,id) >0) # SAME

Essentially, I  would like to preserve all values of an id with more than
one row. Any ideas are much appreciated.

In addition, is there an easy way to create a new variable based on the
number of instances of each id (in the long dataset)? e.g.

id time           x                NEW_VARIABLE
1  1    0 -0.03921791     1
2  2    0 -1.07869262     1
3  2    5  1.73442621     2
4  3    0 -0.64356207     1
5  3    2  1.19691074     2
6  3   10  0.62035225    3

Thank you,

AC

        [[alternative HTML version deleted]]

______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to