【问题标题】:extract only numeric columns from data frame [duplicate]从数据框中仅提取数字列[重复]
【发布时间】:2013-10-16 12:55:31
【问题描述】:

我查看了互联网,但没有找到简单且干净的解决方案。

这是我的 df 的一部分:

structure(list(ID = structure(c(12L, 12L, 12L, 12L, 12L, 12L, 
12L, 12L, 12L, 12L), .Label = c("B0F", "B12T", "B1T", "B21T", 
"B22F", "B26T", "B2F", "B33F", "B3F", "B4F", "B7F", "P1", "P21", 
"P24", "P25", "P27", "P28", "P29"), class = "factor"), Data = structure(c(9646, 
9836, 9938, 10043, 10134, 10203, 10302, 10354, 10421, 10528), class = "Date"), 
    T = c(11.3, 9.7, 9.8, 10.5, 9.9, 10, 10, 10.1, 10, 10), ph = c(6.8, 
    6.9, 7.1, 6.9, 7, 6.93, 7.01, 6.9, 7.01, 6.84), EC = c(1840L, 
    1060L, 940L, 760L, 820L, 1038L, 1035L, 839L, 767L, 433L)), .Names = c("ID", 
"Data", "T", "ph", "EC"), row.names = c(NA, 10L), class = "data.frame") 

这些是变量:

str(df)
'data.frame':   10 obs. of  5 variables:
 $ ID  : Factor w/ 18 levels "B0F","B12T","B1T",..: 12 12 12 12 12 12 12 12 12 12
 $ Data: Date, format: "1996-05-30" "1996-12-06" "1997-03-18" ...
 $ T   : num  11.3 9.7 9.8 10.5 9.9 10 10 10.1 10 10
 $ ph  : num  6.8 6.9 7.1 6.9 7 6.93 7.01 6.9 7.01 6.84
 $ EC  : int  1840 1060 940 760 820 1038 1035 839 767 433

我需要的是一个新的 df,其中包含数字列的原始值(如 T、pH 和 EC)。我知道这可以通过简单的列提取 (new_df=df[,3:5]) 来完成,但我有很多 df 应该在其上完成此操作。

谢谢

【问题讨论】:

    标签: r extract


    【解决方案1】:

    怎么样

    new_df <- df[sapply(df,is.numeric)]
    

    ?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-07-19
      • 2011-12-11
      • 1970-01-01
      • 1970-01-01
      • 2021-11-29
      • 2018-04-08
      • 1970-01-01
      • 2019-11-08
      相关资源
      最近更新 更多