1

I am trying all afternoon to do this:

Convert a dataframe to integer, which works fine: cts <- lapply(cts, function(x) as.integer(x))

But afterwards my rownames(cts) = Null. I want to keep my rownames. How do I exclude them from this conversion?

Thanks so much!!

Bine
  • 63
  • 2
  • 7

1 Answers1

3

Use [] to preserve the attributes i.e rownames here.

cts[] <- lapply(cts, as.integer)

Using mtcars as reproducible example.

df <- mtcars
rownames(df)
df <- lapply(df, as.numeric)
rownames(df)
df <- mtcars
df[] <- lapply(df, as.numeric)
rownames(df)
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213