小编v_h*_*ead的帖子

将矩阵转换为 R 中的数据框

我有以下数字数据框数据集:

x1   x2   x3  ...
1    2    3
...

Run Code Online (Sandbox Code Playgroud)

我对所有列进行了以下应用夏皮罗测试

lshap <- lapply(dataset, shapiro.test)
lres <- t(sapply(lshap, `[`, c("statistic","p.value")))
Run Code Online (Sandbox Code Playgroud)

的输出lres看起来像这样:

               statistic p.value     
Strong         0.8855107 6.884855e-14
Hardworking    0.9360735 8.031421e-10
Focused        0.9350827 6.421583e-10
Run Code Online (Sandbox Code Playgroud)

现在,当我这样做时:

class(lres)
Run Code Online (Sandbox Code Playgroud)

它给了我"matrix" "array"

我的问题是如何转换lres为数据框?

我想要这个输出作为数据框:

variable       statistic p.value  
   
Strong         0.8855107 6.884855e-14
Hardworking    0.9360735 8.031421e-10
Focused        0.9350827 6.421583e-10
...
Run Code Online (Sandbox Code Playgroud)

当我这样做时,to_df <- as.data.frame(lres)我得到以下奇怪的输出:

             statistic   p.value
Strong      <dbl [1]>   <dbl [1]>       
Hardworking <dbl [1]>   <dbl [1]>       
Focused     <dbl [1]>   <dbl [1]>       
Gritty      <dbl [1]> …
Run Code Online (Sandbox Code Playgroud)

r

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

为什么在头文件中使用 typedef 而不仅仅是在 .c 中执行所有操作

我的文件中有一段这样的代码.c

typedef struct stack {
    int maxsize;  
    int top;
    int* items;
} stack;
Run Code Online (Sandbox Code Playgroud)

我读过,当使用头文件时,最好使用这个:

.h

typedef struct stack stack;
Run Code Online (Sandbox Code Playgroud)

并做 .c

struct stack {
    int maxsize;
    int top;
    int* items;
};

Run Code Online (Sandbox Code Playgroud)

我的问题是,为什么我们不能在头文件中只使用函数声明,而 typedef 的事情,我们都在 c 文件中完成?为什么上面将 typedef 和 struct 分离到不同的文件中?

c

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

三次函数根二分搜索超出时间限制

我已经实现了这个解决方案来查找三次函数的根

f(x) = ax3 + bx2 + cx + d

给定abcd,确保它是单调的。

在将解决方案提交给在线法官而没有显示测试用例后,我遇到了时间限制错误。abc、 并d保证该函数是单调的并且我们知道它是连续的。该代码首先找到区间[A, B]使得f(A) * f(B) < 0;然后代码开始实现二分搜索。

我想知道是否有可能最小化我的代码的时间复杂度,以便它通过在线判断。输入是a, b, c, d,输出应该是有错误的根0.000001

代码:

#include <iostream>
#include <algorithm>
//#include <cmath>
//#include <string>

using namespace std;

int f(double a, double b, double c, double d, double x) {
    return x*(x*(a*x + b) + …
Run Code Online (Sandbox Code Playgroud)

c++ numeric bisection

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

调整堆大小时 C 中的 realloc 错误

我想在堆的插入函数中使用 C 中的 realloc。这是代码:

\n
typedef struct MaxHeap {\n    int size;\n    int* heap;\n} MaxHeap;\n\nvoid max_insert(MaxHeap* max_heap, int* key_index, int key) { // O(logn)\n    max_heap->heap = realloc((max_heap->size+1), sizeof * max_heap->heap);\n    max_heap[max_heap->size] = N_INF;\n    max_heap->size += 1;\n    increase_key(max_heap, key_index, max_heap->size, key)\n}\n
Run Code Online (Sandbox Code Playgroud)\n

我收到这个警告:

\n

warning: passing argument 1 of \xe2\x80\x98realloc\xe2\x80\x99 makes pointer from integer without a cast [-Wint-conversion]我尝试了这个修复:

\n
max_heap->heap = realloc((max_heap->heap), (max_heap->size+1) * sizeof(*(max_heap->heap)));\n\n
Run Code Online (Sandbox Code Playgroud)\n

更新

\n

我这样做了:

\n
void max_insert(MaxHeap* max_heap, int* key_index, int key) { // O(logn)\n    int* …
Run Code Online (Sandbox Code Playgroud)

c heap

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

标签 统计

c ×2

bisection ×1

c++ ×1

heap ×1

numeric ×1

r ×1