使用R中的map创建包含绘图列表的数据框

时间:2017-04-19 01:45:56

标签: r purrr

我试图创建一个数据框,其中包含作为列表嵌入数据框中的图,以便我可以查看由行表示的每个模型的两个图。

这是一个显示我的困境的最小例子

public void verifyTable() {

String Test = driver.findElement(loggedinas).getText();


boolean isDue = false;
isDue = UtilClass.isElementPresent(driver, TableContent);

if (isDue) {
    if (isDue = driver.findElement(TableContent).getText() != null)
        ;
    System.out.println("Action Required = " + isDue);

    WebElement table_element = driver.findElement(Table);
    List<WebElement> tr_collection = table_element
            .findElements(TableRow);
    System.out.println("Number Of Due = " + tr_collection.size());

    int row_num, col_num;

    row_num = 1;
    for (WebElement trElement : tr_collection) {
        List<WebElement> td_collection = trElement.findElements(By.xpath("td"));

        System.out.println("--NUMBER OF COLUMNS = "
                + td_collection.size() + "--");

        col_num = 1;
        for (WebElement tdElement : td_collection) {
            // System.out.println("Pending # "+row_num+", col # "+col_num+
            // "text="+tdElement.getText());

            System.out.printf(" # " + tdElement.getText(), " ID "
                    + tdElement.getText());
            col_num++;

        }
        row_num++;

    }

map和bind_rows都以package array; import java.util.Scanner; import java.util.Arrays; public class Array { public static void main(String[] args) { int[] a = new int[10]; Scanner input = new Scanner(System.in); System.out.println("Please enter ten numbers: "); input.nextInt(); for(int j=0; j<10; j++) a[j]=input.nextInt(); System.out.println("Your number list is:"); System.out.println(Arrays.toString(a)); } } }

失败

3 个答案:

答案 0 :(得分:2)

这样做:

  

do.call(rbind,not_df)

答案 1 :(得分:1)

这是tidyverse方法,但我修改了您的示例。如果这是你的目标,请告诉我:

df = data.frame(x=1:5, a=11:15, b=21:25)

df = df %>% group_by(x) %>%
  mutate(p1 = list(ggplot(., aes(a, b)) + geom_point()),
         p2 = list(ggplot(., aes(a, b)) + geom_point())) %>%
  ungroup %>% 
  nest(p1, p2, .key=plots)

df
# A tibble: 5 × 4
        x     a     b            plots
  1     1    11    21 <tibble [1 × 2]>
  2     2    12    22 <tibble [1 × 2]>
  3     3    13    23 <tibble [1 × 2]>
  4     4    14    24 <tibble [1 × 2]>
  5     5    15    25 <tibble [1 × 2]>

答案 2 :(得分:1)

如果您的函数输出tibble(来自tidyverse / package tibble )而不是list,您可以直接使用map_df来获取输出想。

我发现tribble函数在这里更有用,因为tibble导致回收单个值,除非我使用plots列的嵌套列表。

使用tribble

foo2 <- function(x) {
    tibble::tribble(
        ~plots, ~bar, ~x,
        list(qplot(1), qplot(2)), 'bar', x
    )
}

purrr::map_df(1:5, foo2)

# A tibble: 5 × 3
       plots   bar     x
      <list> <chr> <int>
1 <list [2]>   bar     1
2 <list [2]>   bar     2
3 <list [2]>   bar     3
4 <list [2]>   bar     4
5 <list [2]>   bar     5

使用tibbleplots

的嵌套列表
foo <- function(x) {
    tibble::tibble(
        plots = list(list(qplot(1), qplot(2))),
        bar = 'bar',
        x = x
    )
}

purrr::map_df(1:5, foo)

# A tibble: 5 × 3
       plots   bar     x
      <list> <chr> <int>
1 <list [2]>   bar     1
2 <list [2]>   bar     2
3 <list [2]>   bar     3
4 <list [2]>   bar     4
5 <list [2]>   bar     5