Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a Data Frame of Unequal Lengths

Tags:

dataframe

r

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) 

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.

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)))  
like image 876
ATMathew Avatar asked Aug 25 '11 19:08

ATMathew


People also ask

How do I combine two data frames with different number of rows?

Use the left_join Function to Merge Two R Data Frames With Different Number of Rows. left_join is another method from the dplyr package. It takes arguments similar to the full_join function, but left_join extracts all rows from the first data frame and all columns from both of them.

How do you create a DataFrame in R?

How to Create a Data Frame. We can create a dataframe in R by passing the variable a,b,c,d into the data. frame() function. We can R create dataframe and name the columns with name() and simply specify the name of the variables.

How does Rbind work in R?

rbind() function in R Language is used to combine specified Vector, Matrix or Data Frame by rows. deparse. level: This value determines how the column names generated. The default value of deparse.

What is data frame give example?

A data frame is used for storing data tables. It is a list of vectors of equal length. For example, the following variable df is a data frame containing three vectors n, s, b. > n = c(2, 3, 5) > s = c("aa", "bb", "cc")


1 Answers

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

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).

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 

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 
like image 184
Owen Avatar answered Oct 17 '22 18:10

Owen