R 将列转换为行

R 将列转换为行,r,R,我想将列转换为行。因此,我使用函数“重塑”来完成它。更确切地说,我有: `n = c(2, 3, 5) s = c("aa", "bb", "cc") b = c(TRUE, FALSE, TRUE) df = data.frame(n, s, b)` 问题是,对于每个“n”,我有几个“s”值(总是相同的重复) 我使用: reshape(df, idvar = "s", ,direction = "wide", varying = list("b")) 我在某个地方犯了一个错误,但我无

我想将列转换为行。因此,我使用函数“重塑”来完成它。更确切地说,我有:

`n = c(2, 3, 5) 
s = c("aa", "bb", "cc") 
b = c(TRUE, FALSE, TRUE) 
df = data.frame(n, s, b)`
问题是,对于每个“n”,我有几个“s”值(总是相同的重复)

我使用:

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

我在某个地方犯了一个错误,但我无法找出它。

您可以使用
cbind.data.frame()
以及
table()
函数

> 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
>测向
国家年
1澳大利亚1956年
1957年12月2日
3新西兰1958年
>表(cbind.data.frame(df$国家,df$年))
df$年
df$国家1956 1957 1958
澳大利亚100
Ind 0 1 0
新西兰0 0 1

请使用
dput
而不是
图像显示一个可复制的示例。此外,图像不清晰,有很多空白。请查看动物园包装中的na.locf,然后