0

I have experimented with different solutions (including several discussed on stackoverflow and the R help thread) for a couple of days. None has produced a good solution. Hence this posting.

I need to create a data frame

df <- as.data.frame (matrix (nrow = 3470, ncol = 22,000)

The first 3 columns are character / string data. rest are numerical data. The mixed mode screwed up this approach.

df <- data.frame (c1 = char(22,000), c2 = char(22,000), c3 = char(22,000), .....)

Is there any way to set columns 4 to 22,000 as numeric in this solution? (then I can use colnames (df) to set the real names).

While initializing data frames with something like

 df <- data.frame (c1 = char(22,000), c2 = char(22,000), c3 = char(22,000), .....)

is there a mechanism for using something like rep to declare that c4 to c22,000 are numeric?

Thank you very much. (I know initializing data frame has been discussed several times. I tried several approaches, yet to find a good solution). Hence this post to a topic that has been beaten to death.

1 Answer 1

2

I'm not sure where you got that syntax from. First, don't use commas as thousands separators; that's fine for humans, but not for computers. Second, the function to create a character vector is character(n), not char(n). Third, the arguments to data.frame are the columns you want in your data frame, not the rows, so you probably want 3470, not 22000. Finally, the equivalent function for a numeric vector is numeric(n).

df <- data.frame(
    c1 = character(3470),
    c2 = character(3470),
    c3 = character(3470),
    n1 = numeric(3470),
    ...)

If you really have 22000 columns, you can do it by passing a matrix which will be converted into multiple columns:

df <- data.frame(
    c1 = character(3470),
    c2 = character(3470),
    c3 = character(3470),
    n = matrix(0, nrow=3470, ncol=21997))
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.