跨多列二进制数据
嗨,我有数据框,在我的DF值的13列中,我想用1,2替换为0和3,4,用1和删除5。 ?因为需要更改13列
Hi I have dataframe and in 13 columns of my df values are coded from 1 to 5. I want to replace 1,2 with 0 and 3,4 with 1 and drop 5. How I can make a change in my current data without mutating? Because there are 13 columns needed to be changed
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以尝试以下代码(借入@benson23 ,谢谢!)
df> = 3 产生布尔矩阵,由
true
或false
na 或
1
,因为na^0 = 1
和1^na = na = na
,并且此矩阵将播放为屏蔽na
条目,也将布尔值转换为数字You can try the code below (borrow data from @benson23, thanks!)
df >=3
yields boolean matrix consisting ofTRUE
orFALSE
NA^(df == 5)
yields a matrix consisting ofNA
or1
, sinceNA^0 = 1
and1^NA = NA
, and this matrix will play as a maskNA
entries and also turns boolean values to numerics我要么将转换作为两步过程(因为有两个规则),要么编写一个封装您的规则并应用这些函数。我将在以下内容中使用“ dplyr”
突变
,因为这似乎是您正在使用的:这是两个步骤的过程:
在这里使用函数:
这里至关重要。您将功能
Myrule
一个适合您问题域的描述名称。I would either perform the conversion as a two-step process (since there are two rules), or write a function that encapsulates your rules, and apply those. I’ll be using ‘dplyr’
mutate
in the following since that seems to be what you’re using:Here’s the two-step process:
And here it is using a function:
Here it is crucial that you give the function
myrule
a descriptive name that fits your problem domain.假设我们有此刺激的数据框:
基本R
我们可以首先设置
df == 5
na
,并使用逻辑表达式查看值是否更大或等于3(由@Danlooo提出的评论提出)。+(df> = 3)
语法用于将df> = 3
的逻辑输出转换为整数。dplyr
或我们可以在中使用
突变
在组合中使用dplyr
软件包中的组合。输出
数据
这是
dput(df)
,以便于更轻松的数据加载。Let's say we have this stimulated dataframe:
Base R
We can first set
df == 5
toNA
, and use a logical expression to see if values are greater then or equal to 3 (proposed by @danlooo in the comment).The
+(df >= 3)
syntax is used to convert logical output ofdf >= 3
to integer.dplyr
Or we can use the
mutate
withacross
combination in thedplyr
package.Output
Data
Here's the
dput(df)
for easier data loading.