创建不等长的数据帧 [英] Create a Data Frame of Unequal Lengths

查看:111
本文介绍了创建不等长的数据帧的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尽管数据帧列必须具有相同的数字行,但是是否有任何方法来创建不等长的数据帧。我不感兴趣将它们保存为列表的单独元素,因为我经常不得不向人们发送这个信息作为一个csv文件,这是最简单的数据框。

While data frame columns must have the same number rows, is there any way to create a data frame of unequal lengths. I'm not interested in saving them as separate elements of a list because I often have to to email people this info as a csv file, and this is easiest as a data frame.

x = c(rep("one",2))
y = c(rep("two",10))
z = c(rep("three",5))
cbind(x,y,z)

上面的代码, cbind()函数只是循环使用较短的列,以便它们在每列中都有10个元素。我如何改变它,只要长度是2,10和5。

In the above code, the cbind() function just recycles the shorter columns so that they all have 10 elements in each column. How can I alter it just so that lengths are 2, 10, and 5.

我以前做过以下操作,但效率不高。 / p>

I've done this in the past by doing the following, but it's inefficient.

  df = data.frame(one=c(rep("one",2),rep("",8)), 
           two=c(rep("two",10)), three=c(rep("three",5), rep("",5))) 


推荐答案

对不起,这不完全是你问的,但我认为可能有另一种方法你想要什么。

Sorry this isn't exactly what you asked, but I think there may be another way to get what you want.

首先,如果矢量长度不同,数据不是真的表格,是吗?如何将其保存到不同的CSV文件?您还可以尝试允许存储多个对象的ascii格式( json XML )。

First, if the vectors are different lengths, the data isn't really tabular, is it? How about just save it to different CSV files? You might also try ascii formats that allow storing multiple objects (json, XML).

如果你觉得数据真的是表格,你可以填写NAs:

If you feel the data really is tabular, you could pad on NAs:

> x = 1:5
> y = 1:12
> max.len = max(length(x), length(y))
> x = c(x, rep(NA, max.len - length(x)))
> y = c(y, rep(NA, max.len - length(y)))
> x
 [1]  1  2  3  4  5 NA NA NA NA NA NA NA
> y
 [1]  1  2  3  4  5  6  7  8  9 10 11 12

如果你绝对必须做一个 data.frame 与不等的列,你可以颠覆检查,在你自己的危险:

If you absolutely must make a data.frame with unequal columns you could subvert the check, at your own peril:

> x = 1:5
> y = 1:12
> df = list(x=x, y=y)
> attributes(df) = list(names = names(df),
    row.names=1:max(length(x), length(y)), class='data.frame')
> df
      x  y
1     1  1
2     2  2
3     3  3
4     4  4
5     5  5
6  <NA>  6
7  <NA>  7
 [ reached getOption("max.print") -- omitted 5 rows ]]
Warning message:
In format.data.frame(x, digits = digits, na.encode = FALSE) :
  corrupt data frame: columns will be truncated or padded with NAs

这篇关于创建不等长的数据帧的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆