Unable to run commands to execute data.frame

rstudio
#1

I am unable to run below queries to create data.frame dn1 & dn2.

dn1 <- data.frame(1:5, 10:14, 30:34,check.rows = FALSE, dimnames=list(c("R1","R2","R3","R4","R5"),c("C1","C2","C3")))
dn2 <- data.frame(1:5, 10:14, 30:34,row.names=c("R1","R2","R3","R4","R5"),col.names=c("C1","C2","C3"))

It is throwing an error in both the cases: Error in (function (..., row.names = NULL, check.rows = FALSE, check.names = TRUE, :
arguments imply differing number of rows: 5, 3

Kindly help how to fix this errors. Can't we use dimnames and col.names in declaring a dataframe.

0 Likes

#2

Welcome to the community!

I believe this is what you're looking for:

# method 1
dn1 <- data.frame(1:5,
                  10:14,
                  30:34)

dimnames(x = dn1) <- list(c("R1", "R2", "R3", "R4", "R5"),
                          c("C1", "C2", "C3"))

dn1
#>    C1 C2 C3
#> R1  1 10 30
#> R2  2 11 31
#> R3  3 12 32
#> R4  4 13 33
#> R5  5 14 34

# method 2
dn2 <- data.frame(1:5,
                  10:14,
                  30:34,
                  row.names = c("R1", "R2", "R3", "R4", "R5"))

names(x = dn2) <- c("C1", "C2", "C3")

dn2
#>    C1 C2 C3
#> R1  1 10 30
#> R2  2 11 31
#> R3  3 12 32
#> R4  4 13 33
#> R5  5 14 34

Created on 2019-04-06 by the reprex package (v0.2.1)

Check the documentation of data.frame. It does not take the arguments col.names or dimnames.

Hope this helps.

0 Likes

#3

Excellent. Thank you very much!!:smiley:

0 Likes

#4

You're welcome! :slight_smile:

If your question is answered (even if by you), will you please consider marking this thread to be solved?

If you don't know how, please take a look at this:

0 Likes

closed #5

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.

0 Likes