【问题标题】:Dividing each row of a dataframe matrix by a vector with a shorter length in R?将数据帧矩阵的每一行除以 R 中长度较短的向量?
【发布时间】:2021-06-28 01:46:39
【问题描述】:

我正在使用一个数据框矩阵“d”,第一列是时间,其余 5 列包含数值:

structure(c("2016-01-01 00:00:00", "2016-01-01 01:00:00", "2016-01-01 02:00:00", 
"206.000", "208.000", "208.500", "246.000", "246.000", "242.000", 
" 50.000", " 51.000", " 51.000", "207.500", "207.500", "209.000", 
NA, NA, NA), .Dim = c(3L, 6L), .Dimnames = list(NULL, c("timestamp", 
"Crow_education_Omer", "Crow_education_Keisha", "Crow_education_Kate", 
"Crow_education_Winston", "Crow_education_Marlin")))

我想将“d”的第 2 到 6 列除以向量“crow_sqm”

crow_sqm <- c(11857.4, 14173.2, 6320.2, 8659.1, 7561.4)

得到

2016-01-01 00:00:00     206.000   123.000   10.000    207.500   NA
2016-01-02 00:00:00     206.000   123.000   10.000    207.500   NA
2016-01-03 00:00:00     206.000   123.000   10.000    207.500   NA

之前我使用 as.matrix(d) 将数据帧“d”转换为矩阵并删除了 d 的第一列。当我尝试划分 d/crow_sqm 时,出现以下错误:

d/crow_sqm 中的错误:二元运算符的非数字参数

“d”当前显示在数据下,“crow_sqm”显示在值下。不确定错误是来自 NA 值还是其他原因。谁能给点建议?

谢谢。

【问题讨论】:

  • 您能以dput 格式发布示例数据吗?请使用dput(c) 的输出编辑问题

标签: r dataframe vector


【解决方案1】:

你可以使用Map来制作

df[-1] <- Map("/", df[-1], crow_sqm)

这样

> df
            timestamp Crow_education_Omer Crow_education_Keisha
1 2016-01-01 00:00:00          0.01737312            0.01735670
2 2016-01-01 01:00:00          0.01754179            0.01735670
3 2016-01-01 02:00:00          0.01758396            0.01707448
  Crow_education_Kate Crow_education_Winston Crow_education_Marlin
1         0.007911142             0.02396323                    NA
2         0.008069365             0.02396323                    NA
3         0.008069365             0.02413646                    NA

数据

> dput(df)
structure(list(timestamp = c("2016-01-01 00:00:00", "2016-01-01 01:00:00",
"2016-01-01 02:00:00"), Crow_education_Omer = c(206, 208, 208.5
), Crow_education_Keisha = c(246, 246, 242), Crow_education_Kate = c(50,
51, 51), Crow_education_Winston = c(207.5, 207.5, 209), Crow_education_Marlin = c(NA,
NA, NA)), row.names = c(NA, -3L), class = "data.frame")

【讨论】:

    【解决方案2】:

    只需稍加调整,您就可以在不转换为矩阵的情况下做到这一点

    df <- as.data.frame(df)
    cbind(df["timestamp"], df[,-1] %>% 
      mutate(across(everything(), ~as.numeric(.))) %>% 
      purrr::map2_df(crow_sqm, ~ .x/.y))
    
                timestamp Crow_education_Omer Crow_education_Keisha Crow_education_Kate Crow_education_Winston Crow_education_Marlin
    1 2016-01-01 00:00:00          0.01737312            0.01735670         0.007911142             0.02396323                    NA
    2 2016-01-01 00:00:00          0.01754179            0.01735670         0.008069365             0.02396323                    NA
    3 2016-01-01 02:00:00          0.01758396            0.01707448         0.008069365             0.02413646                    NA
    

    如果您直接使用 df,则不需要 mutate(across..,因为在将其转换为矩阵时数据类型可能已更改

    【讨论】:

      【解决方案3】:

      您可以使用sweep

      sweep(type.convert(d[, -1]), 2, crow_sqm, `/`)
      
      #     Crow_education_Omer Crow_education_Keisha Crow_education_Kate Crow_education_Winston
      #[1,]               206.0                   123                10.0                  207.5
      #[2,]               208.0                   123                10.2                  207.5
      #[3,]               208.5                   121                10.2                  209.0
      
      #     Crow_education_Marlin
      #[1,]                    NA
      #[2,]                    NA
      #[3,]                    NA
      

      或者转置。

      t(t(type.convert(d[, -1]))/crow_sqm)
      

      数据是一个矩阵,矩阵只能包含一种类型的数据。第一列不能表示为数字,因此矩阵中的所有值都变为字符类型。 -1 用于删除矩阵中的第一列,type.convert 用于将所有列的值从字符更改为数字。

      【讨论】:

      • 我在将 'c' 重命名为 'd' 后尝试使用以下扫描:e = sweep(d, 2, crow_sqm, '/') 但收到以下错误:FUN 中的错误(x , aperm(array(STATS, dims[perm]), order(perm)), ...) : 二元运算符的非数字参数 不确定 [-1] 是做什么的?你也能解释一下吗?谢谢。
      • 1.你有数据框或矩阵吗? class(d) 返回什么。 2.该错误表明您的列不是数字。如果您提供一个可重现的示例来避免这种混淆,那将会很有帮助。编辑您的帖子以包含dput(d)。 3)这行得通吗? sweep(type.convert(d[, -1]), 2, crow_sqm, `/`)
      • 谢谢。 class(d) 返回“矩阵”“数组”。扫描功能有效。您能否解释一下为什么它以前不起作用以及 type.convert() 在做什么?谢谢。
      • 我编辑了答案以包含一些解释。
      猜你喜欢
      • 2012-11-29
      • 2014-01-02
      • 1970-01-01
      • 2020-05-08
      • 1970-01-01
      • 2013-02-14
      • 1970-01-01
      • 2018-07-15
      相关资源
      最近更新 更多