简体   繁体   English

将数据帧转换为 1/0s

[英]Convert a data frame into 1/0s

I have a data frame of 166 people which I would like to convert to a binary matrix.我有一个 166 人的数据框,我想将其转换为二进制矩阵。 So in the data frame it looks like the following:所以在数据框中它看起来像下面这样:

照片

I am just wondering what code could I use to convert this to binary ie 1/0s我只是想知道我可以使用什么代码将其转换为二进制,即 1/0s

A = absent
N = no 
Y = yes 

All for votes一切为了投票

Any help would be appreciated !任何帮助,将不胜感激 !

I've just created some sample data, If I understand what you are looking for correctly -我刚刚创建了一些示例数据,如果我理解您正在寻找的内容 -

df <-matrix(sample(c("a","n","y"),166*20, replace = TRUE),166,20)
df[df == "a"]=NA
df[df == "y"]= 1
df[df == "n"] = 0    

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将数据框的一列的值转换为 0 和 1? - How to convert the values of a column of a data frame to 0s and 1s? 在R中将一个数据帧转换为另一个具有0和1的数据帧 - convert a data frame to another data frame with 0s and 1s in R 按日期(包括列标题)对数据框列进行分组,并汇总R中1和0的实例 - Group data frame columns by their dates (which comprise column titles) and summarize instances of 1s and 0s in R 如何基于2列数据帧创建1s和0s的矩阵 - How to create a matrix with 1s and 0s based off a 2-column data frame 如何对R中data.frame中两个相邻列的值求和但保持0? - How to sum values from two adjacent columns in a data.frame in R but keep 0s as such? 如何将结果限制为仅包含0的数据框行和列? - How do I limit results to only those data frame rows and columns that contain 0s? 将数据框转换为一个元胞向量 - Convert data frame to one cell vector(s) 当那是一行中唯一的值时,将0s转换为NAs。 使用R保留所有其他0 - Convert 0s to NAs when that is the only value in a row. Keep all other 0s using R 如何将整数转换为数字,并且将0转换为百分之一? - How do I convert integers to numeric and have 0s to the hundredths? 如何在R的数据帧的某些列中将0转换为NaN? - How to convert 0s to NaNs in certain columns of dataframe in R?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM