> On May 17, 2018, at 6:40 AM, Miluji Sb wrote:
>
> Dear William and Ben,
>
> Thank you for your replies and elegant solutions. I am having trouble with
> the fact that two of the previous locations do not appear in current
> locations (that is no one moved to OKC and
Dear William and Ben,
Thank you for your replies and elegant solutions. I am having trouble with
the fact that two of the previous locations do not appear in current
locations (that is no one moved to OKC and Dallas from other cities), so
these two cities are not being included in the output.
I
xtabs does this automatically if your cross classifying variables are
factors with levels all the cities (sorted, if you like):
> x <- sample(letters[1:5],8, rep=TRUE)
> y <- sample(letters[1:5],8,rep=TRUE)
> xtabs(~ x + y)
y
x c d e
a 1 0 0
b 0 0 1
c 1 0 0
d 1 1 1
e 1 1 0
>
Make current_location and previous_location factors with the same set of
levels. The levels could be the union of the values in the two columns or
a predetermined list. E.g.,
> x <- data.frame(previous_location=c("Mount Vernon","Burlington"),
current_location=c("Sedro Woolley","Burlington"))
>
Dear Bert and Huzefa,
Apologies for the late reply, my account got hacked and I have just managed
to recover it.
Thank you very much for your replies and the solutions. Both work well.
I was wondering if there was any way to ensure (force) that all possible
combinations show up in the output.
or in base R : ?xtabs??
as in:
xtabs(~previous_location + current_location,data=x)
(You can convert the 0s to NA's if you like)
Cheers,
Bert
Bert Gunter
"The trouble with having an open mind is that people keep coming along and
sticking things into it."
-- Opus (aka Berkeley Breathed
Dear Miluji,
If I understand correctly, this should get you what you need.
temp1 <-
structure(list(id = 101:115, current_location = structure(c(2L,
8L, 8L, 3L, 6L, 5L, 1L, 2L, 7L, 4L, 2L, 8L, 8L, 3L, 6L), .Label =
c("Austin",
"Boston", "Cambridge", "Durham", "Houston", "Lynn", "New Orleans",
7 matches
Mail list logo