我有这个tbl具有以下形式的结构:
> tbl
a_1 a_2 a_3
[1,] "L" "14" "L"
[2,] "L" "62" "D"
[3,] "H" "0" "L"
Run Code Online (Sandbox Code Playgroud)
这是一个矩阵,实际上:
> class(tbl)
[1] "matrix"
Run Code Online (Sandbox Code Playgroud)
但是当我尝试将其更改为 a 时data.frame,df 的所有条目仅显示数据类型,如下所示:
>as.data.frame(tbl, nrow = length(tbl[,1]), ncol = 3, byrow = TRUE)
a_1 a_2 a_3
<list> <list> <list>
<chr[1]> <chr[1]> <chr[1]>
<chr[1]> <chr[1]> <chr[1]>
<chr[1]> <chr[1]> <chr[1]>
Run Code Online (Sandbox Code Playgroud)
我尝试了多种选择,但似乎都不起作用,包括:
data.frame(rows=rownames(tbl)[row(tbl)],vars=colnames(tbl)[col(tbl)], values=c(tbl))
但是当我尝试使用它时收到一个错误。我希望结果data.frame采用以下形式:
a_1 a_2 a_3
<char> <chr> <char>
"L" "14" "L"
"L" "62" "D"
"H" "0" "L"
Run Code Online (Sandbox Code Playgroud)
我寻找了类似的问题,但找不到任何有同样问题的人。任何建议都会有很大帮助!
dput(tbl)
structure(list("L", …Run Code Online (Sandbox Code Playgroud) 所以我有这个例子df:
df <- dput(structure(list(arts = structure(c(1L, 1L, 3L, 4L), .Label = c("art1", "art2"), class = "character"), scr1 = c(52L, 58L, 40L, 62L), scr2 = c(25L, 23L, 55L, 26L), scr3 = c(36L, 60L, 19L, 22L)), .Names = c("art_id", "scr1", "scr2", "scr3"), row.names = c(NA, -4L), class = "data.frame"))
> df
art_id scr1 scr2 scr3
1 1 52 25 36
2 1 58 23 60
3 3 40 55 19
4 4 62 26 22
Run Code Online (Sandbox Code Playgroud)
我dplyr用来总结的art_id
df …Run Code Online (Sandbox Code Playgroud) 我有这样data.frame的形式,如:
my_df <- data.frame(id = c(1, 1, 2, 3),
title = c("YourMa", "YourMa", "MyMa", "HisMa"),
autqty = c(2, 2, 1, 1),
aut = c("Steve", "Joe", "Albert", "Kevin"),
pubb = c("Good", "Good", "Meh", "Fan"))
Run Code Online (Sandbox Code Playgroud)
看起来像:
> my_df
id title autqty aut pubb
1 YourMa 2 Steve Good
1 YourMa 2 Joe Good
2 MyMa 1 Albert Meh
3 HisMa 1 Kevin Fan
Run Code Online (Sandbox Code Playgroud)
请注意,id 1除了一个aut条目外,所有信息都相同. 我的目标是减少数据my_df,将aut数据合并为一个元素:
id title autqty aut pubb …Run Code Online (Sandbox Code Playgroud)