小编Mat*_*ica的帖子

将数值列的 NA 替换为 R 中的数值和字符值

我有一个包含多列的数据框 df 。
其中两列( AGE 和 SALARY 列)的类型为 double 。
我想用 0 替换 AGE 列的缺失值,
用“未找到”替换 SALARY 列的缺失值。

最有效的方法是什么?

replace_na(df, list(AGE=0, SALARY="not found"))
Run Code Online (Sandbox Code Playgroud)

我收到错误:

Error in `stop_vctrs()`:
! Can't convert `replace$SALARY` <character> to match type of `data$SALARY` <double>.
Backtrace:
 1. tidyr::replace_na(df, list(AGE= 0, SALARY= "not found"))
 2. tidyr:::replace_na.data.frame(df, list(AGE= 0, SALARY= "not found"))
 3. vctrs::vec_assign(...)
 4. vctrs `<fn>`()
 5. vctrs::vec_default_cast(...)
 6. vctrs::stop_incompatible_cast(...)
 7. vctrs::stop_incompatible_type(...)
 8. vctrs:::stop_incompatible(...)
 9. vctrs:::stop_vctrs(...)
Run Code Online (Sandbox Code Playgroud)

编辑:这是我的数据集的来源:https://drive.google.com/file/d/1cKxzNrnIMq4RxdMcBz3nlr7YtYaPhn5_/view ?usp=sharing

r missing-data dataframe na

3
推荐指数
1
解决办法
6483
查看次数

tbl_summary() 未在 R 中显示“是/否”级别

在下面的数据框中,g 变量有两个级别,但是 tbl_summary() 没有显示其级别。

data.frame(a=c(0,1,2),
           
           b=c(0,1,2),
           
           f=c("m", "f", "m"),
           
           g = c("Yes", "No", "Yes"),
           
           output = c(0,1,0)) %>%
  
  tbl_summary(by=output)

  a b f   g output
1 0 0 m Yes      0
2 1 1 f  No      1
3 2 2 m Yes      0
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

我尝试遵循R gtsummary 包并没有在汇总表中显示因子级别,但不幸的是我无法解决这个问题。我会很感激任何提示或帮助吗?

r categorical-data dplyr gtsummary

3
推荐指数
1
解决办法
2709
查看次数

如何在仅在 R 管道 dplyr 中包含上述几行总和的数据帧的特定索引中插入行

对于下面的数据框,

df <- data.frame(id = c(rep(101, 4), rep(202, 3)),
                status = c("a","b","c","d", "a", "b", "c"),
                wt = c(100,200,100,105, 20,22,25),
                ht = c(5.3,5.2,5,5.1, 4.3,4.2,4.1))
    
df
   id status  wt  ht
1 101      a 100 5.3
2 101      b 200 5.2
3 101      c 100 5.0
4 101      d 105 5.1
5 202      a  20 4.3
6 202      b  22 4.2
7 202      c  25 4.1
Run Code Online (Sandbox Code Playgroud)

我想得到下面的输出:

> output
   id status  wt   ht
1 101      a 100  5.3
2 101      b 200  5.2 …
Run Code Online (Sandbox Code Playgroud)

r rows dataframe dplyr tidyverse

2
推荐指数
1
解决办法
104
查看次数

如何在 R 中确保矩阵可逆

该链接具有我的矩阵的 dput 输出结构。
\n https://pastebin.com/TsUzuF4L

\n
\n

solve() 中的错误:系统在计算上是奇异的:R 中的倒数条件数 = 4.35295e-21

\n
\n

我想知道R中是否有任何通用方法可以确定矩阵可逆?有什么功能吗?

\n

我添加了属性tol=FALSEor tol = 1e-22(与 error 中的数字相比),但我仍然收到相同的错误。

\n

附:我将其放在 stackexchange 上的原因是,我的矩阵行列式不为零,但 R 给出了上面的错误,并相信我的矩阵不可逆!怎么会?!

\n

在此输入图像描述

\n

我的矩阵是 45 \xc3\x97 45。dput()输出超出了 Stack Overflow 上 40000 个字符的限制,但为了了解其数字是什么,我在上面显示了其中的一部分。

\n

r matrix matrix-inverse singular

1
推荐指数
1
解决办法
766
查看次数

删除ggplot中Y轴的一部分

我正在尝试应用R/ggplot2:从散点图中折叠或删除 y 轴段,以从绘图的 y 轴中删除 25 到 75 之间的值。但是,当我对图进行分组时,我没有得到所需的结果。
挤压 y 轴的一部分以使绘图清晰的最佳方法是什么?

library(scales)
squish_trans <- function(from, to, factor) {
  
  trans <- function(x) {
    
    if (any(is.na(x))) return(x)

    # get indices for the relevant regions
    isq <- x > from & x < to
    ito <- x >= to
    
    # apply transformation
    x[isq] <- from + (x[isq] - from)/factor
    x[ito] <- from + (to - from)/factor + (x[ito] - to)
    
    return(x)
  }

  inv <- function(x) {
    
    if (any(is.na(x))) return(x)

    # …
Run Code Online (Sandbox Code Playgroud)

r ggplot2

0
推荐指数
1
解决办法
303
查看次数