【问题标题】:R lapply convert to integer but keep rownames [duplicate]R lapply转换为整数但保留行名[重复]
【发布时间】:2020-11-09 20:41:33
【问题描述】:

我整个下午都在尝试这样做:

将数据帧转换为整数,效果很好: cts

但之后我的 rownames(cts) = Null。我想保留我的行名。 如何将他们从此次转化中排除?

非常感谢!!

【问题讨论】:

  • 向我们展示一些可重现的数据

标签: r


【解决方案1】:

使用[] 保留属性,即此处的行名。

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

使用mtcars 作为可重现的示例。

df <- mtcars
rownames(df)
df <- lapply(df, as.numeric)
rownames(df)
df <- mtcars
df[] <- lapply(df, as.numeric)
rownames(df)

【讨论】:

  • 哦哇非常感谢!!所以每当我想保存之前的“设置”“[]”?
  • 是的,它应该在大多数时候保存它。
  • 你也可以使用lapply(cts, as.integer),因为R将as.integer识别为一个函数
  • 非常有帮助,我不知道 :)
  • 如果你定义一个自定义函数,你将使用function(x) 格式
猜你喜欢
  • 2016-11-12
  • 1970-01-01
  • 2014-06-04
  • 2022-08-13
  • 2021-11-30
  • 1970-01-01
  • 1970-01-01
  • 2021-08-10
  • 1970-01-01
相关资源
最近更新 更多