重塑和组合桌子

时间:2019-05-03 13:13:27

标签: r dataframe plyr reshape2

我有一个由三列 private void setDataToUI() { gridLayoutManager = new GridLayoutManager(this,1); recyclerView.setLayoutManager(gridLayoutManager); Log.d(TAG,"Data_List-->"+data_list); adapter = new AdapterImageGallery(this, data_list); recyclerView.setAdapter(adapter); recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() { @Override public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) { super.onScrolled(recyclerView, dx, dy); if (gridLayoutManager.findLastCompletelyVisibleItemPosition() == data_list.size()-1){ fetchData2(data_list.get(data_list.size()-1).getNext()); } } }); } 组成的数据帧DATA_SET和由两列c(TEST1, TEST2, TEST3)组成的第二个数据帧DATA_SET1

c(key, value)

因此,我的意图是使用DATA_SET<-data.frame( TEST1=c(200,220,200,260,300,290,320,320,360,400), TEST2=c(200,220,200,260,400,290,220,370,260,200), TEST3=c(200,220,200,260,500,290,120,240,160,400) ) DATA_SET1<-data.frame( key=c(rep("TEST1",10),rep("TEST2",10),rep("TEST3",10)), value=c(700,700,700,700,700,700,700,700,700,700) ) 函数或类似功能更改DATA_SET1,然后将其与第一个表(即melt())结合使用,以得到如下表。< / p>

enter image description here

2 个答案:

答案 0 :(得分:1)

您可以在base r中进行以下操作。

unstack负责“使用DATA_SET1功能或{s̲o̲m̲e̲t̲h̲i̲n̲g̲ ̲si̲m̲i̲l̲a̲r̲” 更改melt(),而rbind用于“与第一个表(DATA_SET)”

rbind(DATA_SET, unstack(DATA_SET1, form = value ~ key))

#>    TEST1 TEST2 TEST3
#> 1    200   200   200
#> 2    220   220   220
#> 3    200   200   200
#> 4    260   260   260
#> 5    300   400   500
#> 6    290   290   290
#> 7    320   220   120
#> 8    320   370   240
#> 9    360   260   160
#> 10   400   200   400
#> 11   700   700   700
#> 12   700   700   700
#> 13   700   700   700
#> 14   700   700   700
#> 15   700   700   700
#> 16   700   700   700
#> 17   700   700   700
#> 18   700   700   700
#> 19   700   700   700
#> 20   700   700   700

reprex package(v0.2.1)于2019-05-15创建

答案 1 :(得分:0)

使用tidyverse tidyrdplyr

library(tidyr)
library(dplyr)

DATA_SET1 %>%
mutate(id = rep(1:10, 3)) %>%
spread(key, value) %>%
select(-id) %>%
bind_rows(DATA_SET)