I have a data set that looks like the one below. I am trying to write R code to transform it. This is ego network, meaning that the first column has two people who listed their connections (which are in columns A1, A2 and A3). Then in columns 5 through 10 I have interrelationships between people in A1, A2 and A3:
d <- data.frame(matrix(c("Steph","Ellen","John","Jim","Sam","Tom","Sally","Jane","Sam","Jane","Sally","NA","John","Jim","NA","Jane","Sam","NA","NA","Tom"),2,10))
names(d)<-c("Ego","A1","A2","A3","A1Connection1","A1Connection2","A2Connection1","A2Connection2","A3Connection1","A3Connection2")
d
My challenge is to take columns 2 through 10 and make them look like this
ReshapedData<-data.frame(matrix(c("John","John","Sam","Sam","Sally","Sally","Jim","Jim","Tom","Tom","Jane","Jane",
"Sam","Sally","John","NA","Sam","NA","Jane","NA","Jim","Jane","NA","Tom"),12,2))
names(ReshapedData)<-c("Alter", "Alter_Alter")
ReshapedData
I don't need the ego name, at least at this stage. The key is to get the other stuff first. So far the best thing I can come up with is transposing columns 5-10 in each row, and then using rbind to create one long column an then cbind that with list of alters in A1, A2, A3. That has to be some more streamlined way to manage that.
Thanks
Bogdan