现在,我在R中的数据文件中有3个单独的列作为年,月和日.如何将这三列合并为一列并让R了解它是日期?
这就是它现在的样子.
year mon day gnp
1947 1 1 238.1
1947 4 1 241.5
1947 7 1 245.6
1947 10 1 255.6
1948 1 1 261.7
1948 4 1 268.7
Run Code Online (Sandbox Code Playgroud) 我的swapData函数基本上在char*类型的两个节点之间交换数据
17 void swapData(struct Node *node1, struct Node *node2)
18 {
19 // Create a new node "temp" that stores the data of node2
20 struct Node *temp = (struct Node *)malloc(sizeof(struct Node));
21 temp->data = malloc(strlen(node2->data));
22
23 strcpy(temp->data,node2->data);
24
25 // Copy data from node1 to node2
26 strcpy(node2->data,node1->data);
27
28 // Copy data from temp to node1
29 strcpy(node1->data,temp->data);
30
31 free(temp->data);
32 free(temp);
33 }
Run Code Online (Sandbox Code Playgroud)
每当我运行valgrind时,它会不断给我这个输出:
==27570== Invalid write of size 1
==27570== at 0x4C2C00F: strcpy …Run Code Online (Sandbox Code Playgroud) 我试图递归地找到一个列表的总和如下(我知道有一个简单的sum()函数和大量的其他方法):
def rsum(x):
if not (x):
return 0
else:
sum = x[0] + rsum(x.pop(0))
return sum
Run Code Online (Sandbox Code Playgroud)
但是,Python一直告诉我x是一个int而不能有operator [].我该如何解决?
对于那些认为我懒得在这里提出这个问题的人:我已经到处寻找,但仍然不知道如何让Python理解x不应该是一个int.
这是我的函数合并代码:
#include <stdio.h>
#include "merge.h"
void merge(
char a1[], int n1,
char a2[], int n2,
char output[])
{
int i = 0;
int j = 0;
int z = 0;
while (i < n1) || (j < n2) // This is where the error happends
{
if (i < n1) && (j < n2)
if (a1[i] <= a2[j])
output[z++] = a1[i++];
else
output[z++] = a2[j++];
else if (j == n2)
while (i < n1)
output[z++] = a1[i++];
else if (i …Run Code Online (Sandbox Code Playgroud) 我想比较两个链表.你能告诉我为什么我的代码不起作用吗?
如果两个列表不同,则函数返回0,如果它们相同则返回1.
int compare(struct Node *list1, struct Node *list2)
{
struct Node *node1 = list1;
struct Node *node2 = list2;
while ((node1) || (node2))
if (node1->data != node2->data)
return 0;
else {
node1 = node1->next;
node2 = node2->next;
}
if ((node1 == NULL) && (node2 == NULL))
return 1;
else
return 0;
}
Run Code Online (Sandbox Code Playgroud)