【问题标题】:How to create a specific dataframe如何创建特定的数据框
【发布时间】:2018-03-17 10:55:27
【问题描述】:
A_F <- lapply(shortest_paths(graph, from='A', mode ='all', weights = E(graph)$Gewicht, output='epath')$epath,
  function(x) { x$label })

有一个关于数据框的问题。输入此代码后,我有这个output

是否可以将这些数据保存为这样的数据框? dataframe

【问题讨论】:

  • 向您的问题添加数据(不是图片)

标签: r dataframe output


【解决方案1】:

您似乎想将列表转换为数据框。请尝试以下操作:

### creating a list similar to your example
mylist <- list("x1", c("x2", "x6"), c("x1", "x3"), "x2", c("x2", "x6", "x7"))

### determining the length of the longest string in the list 
maxlength <- max(sapply(mylist, length))

### creating a function to set the lengths of all elments in the list to maxlength
myfct <- function(x){
  if(length(x) == maxlength){return(x)}
  if(length(x) != maxlength){return(c(x, rep(NA, maxlength-length(x))))}
}

### here the final output
> t(sapply(mylist, FUN = myfct))
     [,1] [,2] [,3]
[1,] "x1" NA   NA  
[2,] "x2" "x6" NA  
[3,] "x1" "x3" NA  
[4,] "x2" NA   NA  
[5,] "x2" "x6" "x7"

【讨论】:

    猜你喜欢
    • 2016-08-06
    • 2021-09-08
    • 2019-04-07
    • 2018-03-03
    • 1970-01-01
    • 2014-02-07
    • 2019-02-08
    • 2021-11-25
    • 2023-03-20
    相关资源
    最近更新 更多