How to create a 2D array of vectors of different lengths in R programming

+1 vote

I'm new bee to R programming. I  want to know how to create a 2 dimensional array of vectors which are of different lengths. This is what I'm trying to do:

    A = c(1, 2, 3, 4)
    B = c(5, 6, 7)

    C = c(10, 11, 12, 13)
    D = c(14, 15, 16)

    E = c(21, 22, 23, 24)
    F = c(25, 26, 27)

    mat = matrix(nrow=3, ncol=2)

    #This code does not work
    mat[1, 1] = A
    mat[1, 2] = B
    mat[2, 1] = C
    mat[2, 2] = D
    mat[3, 1] = E
    mat[3, 2] = F

I would like to get mat to contain the following:

            [,1]         [,2]
    [1,]   1 2 3 4       5 6 7
    [2,]   10 11 12 13   14 15 16
    [3,]   21 22 23 24   25 26 27

Can someone help me with this?

Feb 1, 2019 in Data Analytics by Tyrion anex
• 8,700 points
1,719 views

1 answer to this question.

0 votes

You can try making a list of matrices like this:

mat<-matrix(list(), nrow=3, ncol=2)
mat[[1,1]] <- c(1, 2, 3, 4)
mat[[1,2]] <- c(5, 6, 7)
mat[[2,1]] <- c(10, 11, 12, 13)
mat[[2,2]] <- c(14, 15, 16)
mat[[3,1]] <- c(21, 22, 23, 24)
mat[[3,2]] <- c(25, 26, 27)
answered Feb 1, 2019 by Sophie may
• 10,620 points

Related Questions In Data Analytics

0 votes
1 answer

How to create dummy variables based on a categorical variable of lists in R?

You can use mtabulate in the following way: library(qdapTools) cbind(data[1], ...READ MORE

answered Apr 13, 2018 in Data Analytics by CodingByHeart77
• 3,750 points
2,660 views
+1 vote
3 answers

How to change the value of a variable using R programming in a data frame?

Try this: df$symbol <- as.character(df$symbol) df$symbol[df$sym ...READ MORE

answered Jan 11, 2019 in Data Analytics by Tyrion anex
• 8,700 points
35,873 views
0 votes
1 answer

How to create a range of dates in R?

Hey, See the below example, > superstore$Order.Date[1] [1] "2014-11-11" > seq(superstore$Order.Date[1],length.out ...READ MORE

answered Oct 29, 2019 in Data Analytics by Cherukuri
• 33,030 points
931 views
+1 vote
1 answer

How to convert a list of dataframes in to a single dataframe using R?

You can use the plyr function: data <- ...READ MORE

answered Apr 14, 2018 in Data Analytics by Sahiti
• 6,370 points
7,108 views
+1 vote
2 answers

How to sort a data frame by columns in R?

You can use dplyr function arrange() like ...READ MORE

answered Aug 21, 2019 in Data Analytics by anonymous
• 33,030 points
1,842 views
+10 votes
3 answers

Which is a better initiative to learn data science: Python or R?

Well it truly depends on your requirement, If ...READ MORE

answered Aug 9, 2018 in Data Analytics by Abhi
• 3,720 points
1,672 views
0 votes
1 answer

How can I perform word stemming in R

The tm package in R provides the stemDocument() function to stem the ...READ MORE

answered Aug 20, 2018 in Data Analytics by Abhi
• 3,720 points
4,586 views
0 votes
1 answer

How to know the type of an object?

To find the type of an object ...READ MORE

answered Sep 20, 2018 in Data Analytics by Abhi
• 3,720 points
728 views
0 votes
2 answers

How to create a table in R without external file?

Tibble also creates a table-like structure. Use below ...READ MORE

answered Sep 3, 2019 in Data Analytics by anonymous
• 33,030 points
3,060 views
0 votes
1 answer

How to convert JSON into CSV in R programming?

Use the jsonlite::fromJSON to read the data into ...READ MORE

answered Jan 24, 2019 in Data Analytics by Sophie may
• 10,620 points
7,118 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP