Home > OS >  How to create a table with only integers
How to create a table with only integers

Time:11-11

I have a table with taxonomy [chr] and presence/absebce data [num] for the sample. I imported it from .tsv file. It is a table with x observations and y variables. I have to convert it to be a table only with integers as entries.

I tried lapply(data,as.integer), it works for columns with presence/absence data, but in the taxonomy column, my data dissapered and I have NA instead of the species. What is more, I don't have a table anymore, but a list with y variables.

Any other way I can do this?

taxonomy site1 site2 site3 site4
species1 0 1 0 0
species2 1 0 0 0
species3 0 1 1 1

After lapply(data,as.integer) it looks like that:

taxonomy site1 site2 site3 site4
NA 0 1 0 0
NA 1 0 0 0
NA 0 1 1 1

CodePudding user response:

Using dplyr, you can do...

library(dplyr)

# everything except the first column
mtcars %>%
  mutate(across(-1, as.integer))

# everything except "taxonomy"
mtcars %>%
  mutate(across(-taxonomy, as.integer))

# everything that starts with "site"
data %>%
  mutate(across(starts_with("site"), as.integer))

I would question what is in your data to make it read as character though in the first place. My guess is there is some "NA" character. If you are using read.table(), you may want to adjust the argument na.strings = "NA" to have it read in as integer in the first place.

CodePudding user response:

If we want to use base R, we can use lapply and assign the output to just a subset of the data:

data[-1]<-lapply(data[-1], as.integer)
data

CodePudding user response:

I think I know where the problem is now - I need to make this first column with taxonomy names as labels in my table.

Reading the data with read.csv('data.csv', header = TRUE, row.names = 1) actually helped.

  • Related