首页 \ 问答 \ 使用R将1-10的向量改变为1-3的向量(Changing vector of 1-10 to vector of 1-3 using R)

使用R将1-10的向量改变为1-3的向量(Changing vector of 1-10 to vector of 1-3 using R)

我使用R来分析调查。 根据受访者回答相应问题的方式,其中一些列包括数字1-10。 我想将1-10比例改为1-3比例。 有一个简单的方法吗? 我正在编写一组复杂的for循环和if语句,但我觉得在R中必须有更好的方法。

我想将数字1-3更改为1; 数字4和8到2; 数字5-7到3,数字9和10到NA。

因此,在下面的代码段中,OriginalColumn将成为NewColumn。

OriginalColumn=c(4,9,1,10,8,3,2,7,5,6)
NewColumn=c(2,NA,1,NA,2,1,1,3,3,3)

没有一堆疯狂的for循环,有没有一种简单的方法可以做到这一点? 谢谢!


I am using R to analyze a survey. Several of the columns include numbers 1-10, depending on how survey respondents answered the respective questions. I'd like to change the 1-10 scale to a 1-3 scale. Is there a simple way to do this? I was writing a complicated set of for loops and if statements, but I feel like there must be a better way in R.

I'd like to change numbers 1-3 to 1; numbers 4 and 8 to 2; numbers 5-7 to 3, and numbers 9 and 10 to NA.

So in the snippet below, OriginalColumn would become NewColumn.

OriginalColumn=c(4,9,1,10,8,3,2,7,5,6)
NewColumn=c(2,NA,1,NA,2,1,1,3,3,3)

Is there an easy way to do this without a bunch of crazy for loops? Thanks!


原文:
更新时间:2022-06-10 09:06

最满意答案

尝试memcpy,而不是strncpy。 strncpy将在值为零的第一个字节处停止复制。 它用于复制空终止字符串,而不是字节数组。 strncpy在第一个零之后用更多的零填充所有字节,这解释了你所看到的


try memcpy, not strncpy. strncpy will stop copying at the first byte that has a value of zero. It is for copying null terminated strings, not an array of bytes. strncpy fills all bytes after the first zero with more zeros, which explains what you are seeing

相关问答

更多
  • 函数先使用后定义了。 C语言规定函数在调用之前要知道函数的具体定义。修改一下函数的实现顺序就可以了。 Converse和Exchange的定义放在main函数之前即可。
  • 我要做的是根据其定义2D阵列的结构: 列数 行数 指向内存中数组数据的指针 请注意,我将“线性化”数组,即分配单个内存块大小为Columns * Rows * sizeof(int) ,并且给定i和j行和列索引,这两个索引可以使用简单的数学转换为a 1D数组中的单个索引(例如index = rowIndex * Columns + columnIndex ) 然后,我将从ReadTxtFile函数返回一个指向此结构的指针: struct IntArray2D { int Rows; int ...
  • 添加单独的静态模式规则 _build/%.o: %.c @mkdir -p _build @echo @echo Compiling $< $(CC) $(CFLAGS) $< -o $@ _build/one.o _build/two.o: _build/%.o: %.h 更好的是,使用依赖关系生成 OBJS := $(addprefix _build/,main.o one.o two.o) DEPS := $(OBJS:.o=.d) CPPFLAGS := -MM ...
  • XBT_LOG_NEW_DEFAULT_CATEGORY(tuto, "...")定义了一个日志记录类别,因此您只能在一个.c文件中使用它。 您可以在另一个文件中声明(并使用)此类别: XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(tuto); 一旦在文件中调用了其中一个宏,就可以在此文件中使用XBT_INFO(...)和朋友。 XBT_LOG_NEW_DEFAULT_CATEGORY(tuto, "...") defines a logging category so you sh ...
  • 作为替代方案,您可以将compile-command指定为缓冲区局部变量。 例如,要编译,运行输出到编译缓冲区,删除可执行文件aftwerard,可以添加 /* -*- compile-command: "gcc -std=gnu11 file-name.c && a.out && rm a.out" -*- */ 到文件的第一行( -*-之间的所有内容都是由...分隔的缓冲区局部变量)。 这个变量在访问缓冲区时被初始化。 然后运行compile将使用此命令。 这对于在任何文件/模式下(其中第一行的注释都 ...
  • 我不认为我在互联网上看到过任何关于这类问题的明确答案,但你可能会对“ 扩展和嵌入PHP ”一书的某些段落感兴趣,这可能是编写PHP时的参考书。扩展,以及PHP引擎的内部。 引用第1章“PHP生命周期”的一对有趣的句子是: 在常见的Web服务器环境中,您永远不会显式启动PHP解释器; 你将启动Apache或其他一些将根据需要加载PHP和处理脚本的Web服务器...... 而且,就在之后: ... CLI二进制文件的行为方式实际上是一样的。 在系统提示符下输入的php命令启动“命令行API”,该命令行API用作 ...
  • 尝试memcpy,而不是strncpy。 strncpy将在值为零的第一个字节处停止复制。 它用于复制空终止字符串,而不是字节数组。 strncpy在第一个零之后用更多的零填充所有字节,这解释了你所看到的 try memcpy, not strncpy. strncpy will stop copying at the first byte that has a value of zero. It is for copying null terminated strings, not an array of ...
  • 将文件扩展名“main.cpp”更改为“main.c”表示编译器不处理C ++文件,而是处理C文件。 编译C与编译C ++不同,因此您需要更改构建规则以匹配,可能您还必须重写文件中的代码以符合C标准(C ++标准不同),并且您可能会有将C启动器链接到其他C ++项目非常困难,因此您可能必须找到替代方案(或者如果您有源代码则重写它们)。 您不能只更改文件的名称,并希望自动完成此工作。 它相当于将'myfile.docx'更改为'myfile.pdf',并认为该文件的内部将自身重写为Micosoft Word文 ...

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。