我的结构tbl
的格式为:
> tbl
a_1 a_2 a_3
[1,] "L" "14" "L"
[2,] "L" "62" "D"
[3,] "H" "0" "L"
这是一个矩阵,实际上是:
> class(tbl)
[1] "matrix"
但是当我尝试将其更改为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]>
我尝试了很多选项,但似乎没有一项可行,包括:
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"
我找了类似的q,但找不到有同样问题的人。任何建议都会有很大的帮助!
dput(tbl)
structure(list("L", "L", "H", "14", "62", "0", "L", "D", "L"), .Dim = c(3L, 3L), .Dimnames = list(NULL, c("a_1", "a_2", "a_3")))
str(tbl)
List of 9
$ : chr "L"
$ : chr "L"
$ : chr "H"
$ : chr "14"
$ : chr "62"
$ : chr "0"
$ : chr "L"
$ : chr "D"
$ : chr "L"
- attr(*, "dim")= int [1:2] 3 3
- attr(*, "dimnames")=List of 2
..$ : NULL
..$ : chr [1:3] "a_1" "a_2" "a_3"
答案 0 :(得分:4)
试试这个单行:
<script type='text/javascript'>
$(document).ready(function () {
var max_fields = 3; //maximum input boxes allowed
var x = 1; //initlal text box count
$('#add-phone').click(function () {
if(x < max_fields){ //max input box allowed
x++; //increment when a new div(section) is added.
//content of added section(div)
$("#add-request").after('<div class="col-md-12" id="site-phone"><select name="phone-manufacture" id="phone-manufacture" class="phone-manufacture"> <option value="apple">Apple</option><option value="sam">Samsung</option></select></div>');
}
});
$(document).on('change','.phone-manufacture', function(){
var method = $('option:selected').val();
if (this.value == 'apple'){
//show different content(Fields, text or another select)
}else if(this.value == 'sam'){
//show something else or append a new element..
}
});
});
</script>
或者这个:
as.data.frame(apply(tbl, 2, unlist))