按特定顺序合并R中的数据框

时间:2017-12-01 10:41:54

标签: r

我仍然很擅长操纵R中的数据并遇到一个看似简单的问题,我无法弄清楚如何解决。

我有3个数据框,每个数据框有9列和720行。

我需要将它们合并为一个包含9列和(3x720)行的数据帧,其中最终数据帧中的前3行是每个原始数据帧的第一行,依此类推所有行中的所有行原始数据框架。

数据框的名称

ALT1

ALT2

ALT3

2 个答案:

答案 0 :(得分:2)

您可以在行号的数据框中创建新列,然后将其与rbind合并,然后根据代表原始行号的rn列对其进行排序数据帧。请注意,rbind仅在数据框具有相同列名时才有效。

alt1 $rn <- seq.int(nrow(alt1))
alt2$rn <- seq.int(nrow(alt2))
alt3$rn <- seq.int(nrow(alt3))

df <- rbind(alt1, alt2, alt3)
df[order(df$rn),]

让我知道这是否有效。

答案 1 :(得分:0)

我假设所有3个数据集中的列名相同。

//here can be any annotation which will enable MVC/Boot 
@Configuration
public class YourConfiguration{

    @Autowired
    private ObjectMapper mapper;

    @Autowired
    private ProviderService providerService;

    @Override
    public void setup() {
        super.setup();
        SimpleModule module = new SimpleModule();
        module.setDeserializerModifier(new BeanDeserializerModifier() {
            @Override
            public JsonDeserializer<?> modifyDeserializer(DeserializationConfig config, BeanDescription beanDesc, JsonDeserializer<?> deserializer) {

                if (beanDesc.getBeanClass() == OrderProductInterface.class) {
                    return new OrderProductInterfaceDeserializer(providerService, beanDesc);
                }
                return deserializer;
            }
        });

        mapper.registerModule(module);
    }

    public static class OrderProductInterfaceDeserializer extends AbstractDeserializer {

            private static final long serialVersionUID = 7923585097068641765L;

            private final ProviderService providerService;

            OrderProductInterfaceDeserializer(roviderService providerService, BeanDescription beanDescription) {
                super(beanDescription);
                this.providerService = providerService;
            }

            @Override
            public Object deserializeWithType(JsonParser p, DeserializationContext context, TypeDeserializer typeDeserializer) throws IOException {
                ObjectCodec oc = p.getCodec();
                JsonNode node = oc.readTree(p);

                //Let's image that we have some identifier for provider type and we want to detect it
                JsonNode tmp = node.get("providerId");
                Assert.notNull(tmp, "'providerId' is mandatory field");
                String providerId = tmp.textValue();
                Assert.hasText(providerId, "'providerId' can't be empty");

                // Modify node
                ((ObjectNode) node).put("providerType",providerService.getProvider(providerId));

                JsonFactory jsonFactory = new JsonFactory();
                JsonParser newParser = jsonFactory.createParser(node.toString());
                newParser.nextToken();

                return super.deserializeWithType(newParser, context, typeDeserializer);

           }

      }
}

这是为了仔细检查您是否获得了每个数据集的第一行。您最后可以使用# create example dataset set.seed(1) alt1 = data.frame(x = rnorm(4), y = rnorm(4)) alt2 = data.frame(x = rnorm(4), y = rnorm(4)) alt3 = data.frame(x = rnorm(4), y = rnorm(4)) library(dplyr) library(purrr) c("alt1", "alt2", "alt3") %>% # get names of datasets map_df(function(x) {d = get(x); d$row_index = 1:nrow(d); d}) %>% # for each name: get data, add a column with the name and bind them all arrange(row_index) # order by row index # x y row_index # 1 -0.62645381 0.32950777 1 # 2 0.57578135 -0.62124058 1 # 3 -0.01619026 0.91897737 1 # 4 0.18364332 -0.82046838 2 # 5 -0.30538839 -2.21469989 2 # 6 0.94383621 0.78213630 2 # 7 -0.83562861 0.48742905 3 # 8 1.51178117 1.12493092 3 # 9 0.82122120 0.07456498 3 # 10 1.59528080 0.73832471 4 # 11 0.38984324 -0.04493361 4 # 12 0.59390132 -1.98935170 4 删除不必要的列。

如果您只在工作区中加载了这3个数据集,则可以将第一行%>% select(-row_index)替换为c("alt1", "alt2", "alt3"),以获得完全自动化的解决方案。