列表列表中存储的数据框中的移动列

时间:2019-01-27 07:05:00

标签: r

我有以下问题:

如果我在列表中存储了多个数据帧,并且我想将最后两列移动到第一列和第二列之间的位置。 所有数据帧中的第一和第二索引始终相同,但最后两列的索引始终不同。

我给出两个数据帧的示例:

df1

c1. c2 c3 c4 c5  c6
1.  4. a.  d. 7. 10
2.  5. b.  e. 8. 11
3.  6. c.  f. 9. 12

Output

df1

c1. c5  c6 c2 c3 c4 
1.  7. 10  4. a.  d. 
2.  8. 11  5. b.  e. 
3.  9. 12  6. c.  f. 

df2

c1. c2 c2_1 c3  c3_1 c4  c5  c6
1.  4.  a1. b5.  d.  h    7. 10
2.  5.  b.  7g   e.  j    8. 11
3.  6.  c.  9r   f.  l    9. 12

Output:

c1. c5  c6  c2 c2_1 c3  c3_1 c4  
1.   7. 10  4.  a1. b5.  d.  h    
2.   8. 11  5.  b.  7g   e.  j    
3.   9. 12  6.  c.  9r   f.  l    

df1和df2存储在list_df

list_df<-list(df1,df2)

我写了这样的东西,但似乎不起作用:

list_new<-list()
for (i in 1:length(list_df)){
  list_new[[i]]<- function(x) {cbind(x[[i]][,1], x[[i]][,ncol(x[[i]]-2)], x[[i]][,2:ncol(x[[i]])])
}}

5 个答案:

答案 0 :(得分:1)

首先,您需要使用purrr::map()lapply()对列表的每个单元格执行特定功能。然后通过dplyr::select(),使用c(1, ncol(.)-1, ncol(.))确定前三列的索引,everything()代表其余列。

library(tidyverse)
map(df.list, ~ select(., c(1, ncol(.)-1, ncol(.)), everything()))

$df1
  c1. c5 c6 c2 c3 c4
1   1  7 10  4 a. d.
2   2  8 11  5 b. e.
3   3  9 12  6 c. f.

$df2
  c1. c5 c6 c2 c2_1  c3 c3_1 c4
1   1  7 10  4  a1. b5.   d.  h
2   2  8 11  5   b.  7g   e.  j
3   3  9 12  6   c.  9r   f.  l

这是基本方法。如果您担心效率问题,我认为它将是最快的解决方案。

lapply(df.list, function(x) x[c(1, ncol(x)-1, ncol(x), 2:(ncol(x)-2))])

数据

df.list <- list(df1 = structure(list(c1. = c(1, 2, 3), c2 = c(4, 5, 6), 
    c3 = structure(1:3, .Label = c("a.", "b.", "c."), class = "factor"), 
    c4 = structure(1:3, .Label = c("d.", "e.", "f."), class = "factor"), 
    c5 = c(7, 8, 9), c6 = 10:12), class = "data.frame", row.names = c(NA, -3L)),
    df2 = structure(list(c1. = c(1, 2, 3), c2 = c(4, 5, 6), 
    c2_1 = structure(1:3, .Label = c("a1.", "b.", "c."), class = "factor"), 
    c3 = structure(c(3L, 1L, 2L), .Label = c("7g", "9r", "b5."
    ), class = "factor"), c3_1 = structure(1:3, .Label = c("d.", 
    "e.", "f."), class = "factor"), c4 = structure(1:3, .Label = c("h", 
    "j", "l"), class = "factor"), c5 = c(7, 8, 9), c6 = 10:12), class = "data.frame", row.names = c(NA, -3L)))

答案 1 :(得分:1)

一种简洁的header('Content-Type: application/json'); if( isset($_POST['name']) && isset($_POST['mobile']) ){ $biz_name = mysql_real_escape_string(trim($_POST['name'])); $biz_phone = mysql_real_escape_string(trim($_POST['mobile'])); $biz_banner = $_FILES['imagebanner']['name']; $biz_banner_tmp = $_FILES['imagebanner']['tmp_name']; move_uploaded_file($biz_banner_tmp, "../profile_banner/$biz_phone.jpg"); echo "acknowledgement"; exit(); } 方法:

data.table

如果关注速度,以下是当前答案的基准:

library(data.table)
lapply(list_df, function(x) setcolorder(x, c(1, ncol(x)-1, ncol(x))))

答案 2 :(得分:1)

lengths能够计算列表中的data.frame列数。然后使用lapply设计所需的索引。

ind <- lapply(lengths(df.list), function(x) c(1, (x-1), x, 2:(x-2)))
Map("[", df.list, ind)

输出:

$df1
  c1. c5 c6 c2 c3 c4
1   1  7 10  4 a. d.
2   2  8 11  5 b. e.
3   3  9 12  6 c. f.

$df2
  c1. c5 c6 c2 c2_1  c3 c3_1 c4
1   1  7 10  4  a1. b5.   d.  h
2   2  8 11  5   b.  7g   e.  j
3   3  9 12  6   c.  9r   f.  l

答案 3 :(得分:0)

使用dplyrpurrr库的示例:

library(dplyr)
library(purrr)

list_df %>%
  map(~select(.x, 1, ncol(.x) - 1, ncol(.x), 4:ncol(.x) - 2))

答案 4 :(得分:0)

使用this answer中的函数var page; var dataTable = $('#masterDataTable').on('preXhr.dt', function (e, settings, data) { data.page = page; }).DataTable( { drawCallback: function(){ $('.paginate_button.first:not(.disabled)', this.api().table().container()) .on('click', function(){ page = 'first'; console.log(page); }); $('.paginate_button.previous:not(.disabled)', this.api().table().container()) .on('click', function(){ page = 'previous'; console.log(page); }); $('.paginate_button.next:not(.disabled)', this.api().table().container()) .on('click', function(){ page = 'next'; console.log(page); }); $('.paginate_button.last:not(.disabled)', this.api().table().container()) .on('click', function(){ page = 'last'; console.log(page); }); }, "processing": true, "serverSide": true, "searching": false, "ajax": { "url":base_path +'/other_path', "dataType":"json", "type":"POST", "data":{"_token":csrf_token,"page":page} }, "columns":[ {"data":"name"}, {"data":"address"}, {"data":"city"}, {"data":"action","searchable":false,"orderable":false} ], "aLengthMenu": [50, 100, 250, 500], "pageLength": 50, "ordering": false, "pagingType": "full" } ); 和Darren的样本数据:

move

功能

map(df.list, move, one_of("c5","c6"), 2)
# $df1
#   c1. c5 c6 c2 c3 c4
# 1   1  7 10  4 a. d.
# 2   2  8 11  5 b. e.
# 3   3  9 12  6 c. f.
# 
# $df2
#   c1. c5 c6 c2 c2_1  c3 c3_1 c4
# 1   1  7 10  4  a1. b5.   d.  h
# 2   2  8 11  5   b.  7g   e.  j
# 3   3  9 12  6   c.  9r   f.  l