合并中的列无序()

时间:2017-01-16 01:11:51

标签: r join dplyr plyr

我正在尝试根据公共列将数据框employeeId加入bar,然后将原始列顺序保留在foo中:

foo

联接列位于> head(foo) city course1 1 Aalborg JEMES 2 Aarhus EM-SANF 3 Aix-en-Provence EMLE 4 Almaty IMRCEES 5 Alnarp SUFONAMA 6 Amsterdam ATOSIM > colnames(foo) [1] "city" "course1" > head(bar) code website 1 4CITIES http://www.4cities.eu/ 2 ACES http://www.sams.ac.uk/aces-erasmus 3 ADVANCES http://www.socialworkadvances.org/ 4 AMASE http://www.amase-master.net/ 5 ARCHMAT http://www.erasmusmundus-archmat.uevora.pt/ 6 ASC http://www.master-asc.org/ > colnames(bar) [1] "code" "website" 中的coursefoo中的code。我使用了以下公式:

bar

此操作失败并产生以下错误消息:

test <- merge(x = foo, y = bar, by.x = "course1", by.y = "code", all.x=TRUE)[, union(names(foo), names(bar))]

我找到了这个解决方案here,但它没有用,即使没有列名重复。可能是什么问题呢?

简单连接有效(无需重新排序),但将连接列置于前面:

Error in `[.data.frame`(merge(x = foo, y = bar, by.x = "course1", by.y = "code",  : 
  undefined columns selected

我尝试添加> head(test) course1 city website 1 JEMES Aalborg http://www.jemes-cisu.eu/ 2 JEMES Aveiro http://www.jemes-cisu.eu/ 3 JEMES Hamburg http://www.jemes-cisu.eu/ 4 EM-SANF Aarhus http://www.emsanf.eu/UK/ 5 EM-SANF Wageningen http://www.emsanf.eu/UK/ 6 EM-SANF Debrecen http://www.emsanf.eu/UK/ 并删除sort = F,但这不起作用。问题是我的实际数据帧有更多列,并且将经历多个连接,因此我想在一个函数中保留所有列中的列顺序。是否存在已知的有效变通方法或保留连接中的列顺序的包?

all.x = TRUE

1 个答案:

答案 0 :(得分:1)

您的重建索引([,union(names(foo), names(bar))])应该归咎于:因为names(bar) "code"不存在allnames <- union(names(foo), recode(names(bar), code = "course1")) merge(foo, bar, by.x = "course1", by.y = "code", all.x = TRUE)[,allnames] ,您将收到索引错误。这是更正后的代码:

foo <- structure(list(city = c("Aalborg", "Aarhus", "Aix-en-Provence", 
    "Almaty", "Alnarp", "Amsterdam"), course1 = c("JEMES", "EM-SANF", 
    "EMLE", "IMRCEES", "SUFONAMA", "ATOSIM")), .Names = c("city", 
    "course1"), class = "data.frame", row.names = c(NA, -6L))
bar <- structure(list(code = c("4CITIES", "ACES", "ADVANCES", "AMASE", 
    "ARCHMAT", "ASC"), website = c("http://www.4cities.eu/", "http://www.sams.ac.uk/aces-erasmus", 
    "http://www.socialworkadvances.org/", "http://www.amase-master.net/", 
    "http://www.erasmusmundus-archmat.uevora.pt/", "http://www.master-asc.org/"
    )), .Names = c("code", "website"), row.names = c(NA, 6L), class = "data.frame")

由于您的示例不可重现(此处合并为空,没有任何共同点),我将使用修改后的结构进行演示:

bar$code

现在确保foo$course中的值set.seed(42) bar$code <- sample(foo$course1) 中存在{<1}}:

allnames <- union(names(foo), recode(names(bar), code = "course1"))
merge(foo, bar, by.x = "course1", by.y = "code", all.x = TRUE)[,allnames]
#              city  course1                                     website
# 1       Amsterdam   ATOSIM                      http://www.4cities.eu/
# 2          Aarhus  EM-SANF          http://www.socialworkadvances.org/
# 3 Aix-en-Provence     EMLE                http://www.amase-master.net/
# 4          Almaty  IMRCEES http://www.erasmusmundus-archmat.uevora.pt/
# 5         Aalborg    JEMES                  http://www.master-asc.org/
# 6          Alnarp SUFONAMA          http://www.sams.ac.uk/aces-erasmus

结果:

import pygame, sys, os
from pygame.locals import *

pygame.init()
screen = pygame.display.set_mode((400, 300))

# font-related code:

fpsClock = pygame.time.Clock()
# https://www.behance.net/gallery/31268855/Determination-Better-Undertale-Font
font = pygame.font.Font(os.path.join("res", "fonts", 'DeterminationMonoWeb.ttf'), 16)

screen.blit(font.render(text, 0, (255, 240, 230)), (10, 10))
pygame.display.flip()
# Main game loop
while True:
    if pygame.event.wait().type in (QUIT, KEYDOWN, MOUSEBUTTONDOWN):
        break

    pygame.display.update()
    fpsClock.tick(60)