How do I make tables smaller from a data.frame in R?

Asked

Viewed 436 times

5

I have a table in data frame, with 100 animals, but with 500 observations of various characteristics. as I do to have subtabels, with only the animals and the characteristic x, where q should show tds the 500 observations of this characteristic X. Animal detail is as factor with 100 levels, and observations are as levels tbem, I wanted that it had only as normal numbers, contains point (3.2 4.2 )

guy animal 1 1.1 2.0 1.0 0.2 1.7 .... (500 times) animal 2

Thank you.

  • I was left with a little doubt, about your question, you want to filter your data and transform the objects or? Maybe you can provide an example of your data.

1 answer

4

Regarding the format of the data, if they are as factor, the ideal is to correct this in the reading of the data. Numerical values read as factors may occur for any of the following reasons:

  • Your table is transposed: For R, the data must have variables (in your case, characteristics) in the columns, and observations (animals) in the rows. Thus, all columns will have the same type of data (number, text, logic, date, etc).

  • There is content that is not part of the data, such as comment at the beginning or end of the data.

  • You have not read the data correctly. The argument stringsAsFactors is useful to prevent texts from seeing factors (but should not be required for numbers), and it may also be necessary to use skip or row.names. We can only help you better if you provide the original data, or a representative sample of it.

To separate only one feature (column), you can do it in several ways, but the simplest is to use the operator $, as follows:

animais$peso

This will return all observations of the weight column, in the form of a vector.

If you cannot solve the import problem, you can convert factors to numbers as follows:

animais$peso <- as.numeric(as.character(animais$peso))

Or, a little more efficient:

animais$peso <- as.numeric(levels(animais$peso))[animais$peso]

Browser other questions tagged

You are not signed in. Login or sign up in order to post.