-1

I would like to convert a column into row. Thus I use the function "reshape" to do it. More precisely, I have:

`n = c(2, 3, 5) 
s = c("aa", "bb", "cc") 
b = c(TRUE, FALSE, TRUE) 
df = data.frame(n, s, b)`

The thing is that I have several values of "s" (always the same are repeated) for each "n".

I use:

reshape(df, idvar = "s", ,direction = "wide", varying = list("b"))

I am making a mistake somewhere but I cannot figure it out.

J. Perez
  • 117
  • 1
  • 2
  • 7
  • Please show a reproducible example using `dput` and not with `images`. Also, the image is not clear with lots of blanks. – akrun Jun 01 '16 at 10:34
  • Look into na.locf from zoo package, then [reshape data from long to wide format](http://stackoverflow.com/questions/5890584) – zx8754 Jun 01 '16 at 10:43

1 Answers1

0

You can make use of cbind.data.frame() along with table() function

> df <- data.frame(Country = c("Aus","Ind","NZ"), Year= c(1956,1957,1958))
> df
   Country Year
 1     Aus 1956
 2     Ind 1957
 3      NZ 1958

> table(cbind.data.frame(df$Country,df$Year))
               df$Year
df$Country 1956 1957 1958
       Aus    1    0    0
       Ind    0    1    0
       NZ     0    0    1
Sowmya S. Manian
  • 3,723
  • 3
  • 18
  • 30