重塑数据框以列出唯一ID上的值-来回

时间:2019-07-24 11:23:06

标签: r reshape

我想压缩数据框中的信息以减少行数。 考虑数据框:

public void requestOnce(Properties p) {
    int retryNumber = 0;
    boolean start = true;
    String destDir = p.getProperty("target_foldername");
    String finalPath = "";

    while (start) {
        try {
            if (retryNumber < Integer.parseInt(p.getProperty("download_retry_number"))) {

                String name = "";
                if(p.getProperty("target_foldername").equalsIgnoreCase("none") || p.getProperty("target_foldername")==null|| p.getProperty("target_foldername").length()<= 0) {
                     name = fm.createName(p) + "." + p.getProperty("source_file_extension");
                }else {
                     name = "/" + fm.createName(p) + "." + p.getProperty("source_file_extension");
                }

                name = name.trim();
                String path = p.getProperty("target_foldername") + name;
                finalPath = path;
                File out = new File(path);
                URL url = new URL(p.getProperty("source_url"));


                HttpURLConnection http = (HttpURLConnection) url.openConnection();
                BufferedInputStream in = new BufferedInputStream(http.getInputStream());

                FileOutputStream fos = new FileOutputStream(out);
                BufferedOutputStream bout = new BufferedOutputStream(fos, 1024);
                byte[] buffer = new byte[1024];
                int read = 0;
                while ((read = in.read(buffer, 0, 1024)) >= 0) {
                    bout.write(buffer, 0, read);

                }
                bout.close();
                in.close();
                start = false;

            }
        } catch (IOException ex) {
            ex.printStackTrace();
            retryNumber++;
            if (retryNumber >= Integer.parseInt(p.getProperty("download_retry_number"))) {
                start = false;
            }
        }

        if(p.getProperty("source_file_extension").equalsIgnoreCase("zip")) {
            try {
                String zipFilePath = finalPath;
                File destp = new File(destDir);

                fm.unzip(zipFilePath, destp);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
       if (retryNumber >= Integer.parseInt(p.getProperty("download_retry_number"))) {
        boolean doesExist = true;
        ArrayList<String> finalDistinctLinkList = allLinks(p);
        for (String link : finalDistinctLinkList) {
            if (p.getProperty("source_url").equalsIgnoreCase(link)) {
                doesExist = true;
                break;
            } else {
                doesExist = false;
            }
        }
        if (!doesExist) {
            System.out.println("The source URL download link from " + p.getProperty("confFile")
                    + " doesn't exist. One of the following links could meet your requirements");
            for (String link : finalDistinctLinkList) {
                System.out.println(link);
            }

        }
    }

}

我想将数据框折叠为“ id”的所有唯一值,然后在变量b中列出这些值。结果应该像

df <- data.frame(id=c("A","A","A","B","B","C","C","C"),b=c(4,5,6,1,2,7,8,9))
df 
  id b
1 A 4
2 A 5
3 A 6
4 B 1
5 B 2
6 C 7
7 C 8
8 C 9

第一步的solution是:

df.results <- data.frame(id=c("A","B","C"),b=c("4,5,6","1,2","7,8,9"))
df.results
  id     b
1  A 4,5,6
2  B   1,2
3  C 7,8,9

您如何将df.results转换回df?

0 个答案:

没有答案