3
我在网上找到了这个,并使用它与我的数据:
df <- data.frame(seasons = c("Season1","Season2","Season3","Season4"))
for(i in unique(df$seasons)) {
df[[paste0(i)]] <- ifelse(df$seasons==i,1,0)
}
唯一的挑战是在结果单元格中有一个0,我想从数据帧中填充一个有意义的值,该数据帧的数据排列如下:
有三个步骤,决定重建原矩阵,称其为S:
# Make square matrix of zeros
rc <- length(unique(df[[1]]) ) # going to assume that number of unique values is same in both cols
S <- diag(1, rc,rc)
# Label rows and cols
dimnames(S) <- list( sort(unique(df[[1]])), sort( unique(df[[2]])) )
# Assign value to matrix positions based on values of df[[3]]
S[ data.matrix( df[1:2]) ] <- # using 2 col matrix indexing
df[[3]]
# -------
> S
Season1 Season2 Season3
Season1 1 3 0
Season2 0 1 4
Season3 5 0 1
它现在是一个真正的矩阵,而不是一个数据框架。
相关内容
- 在python (web2py)上为所有具有相似名称的变量创建一个循环,使我的值未排序
- 如何为具有相似名称的变量列表创建typescript接口?
- r语言 - 如何从相似数据框架创建相似矩阵?
- R创建一个相似表
- 如何获得由循环创建的具有连接名称和相似资源的真实名称的资源的最终列表?
- 如何规范化和创建相似矩阵在Pyspark?
- 在python中使用Gensim Fasttext创建单词列表的成对相似度矩阵
- 从两个数据框架的两个列中的相似值创建新的数据框架
- 使用php gd或imagemagick将大小从4764px调整为960px,以kb为单位创建相似大小的图像(970kb
- 如何使用w2v创建词间相似度矩阵
- 如何创建名称相似列?
- 在python中创建NxN相似/距离矩阵的有效方法
- 从数据框中筛选行,并创建仅具有相似值的新数据框
- r语言 - 在不同的数据框架中找到相似的列值,然后创建一个新的数据框架
- r语言 - 从data.frame中创建三列的不相似矩阵
- 如何比较两个具有相似元素的不同列表,并使用循环创建一个没有相似元素的新列表
- 在salesforce中,如何创建两个具有相似字段的同步对象
- 如何对相似的字符串进行分组,并为R中的每个组创建索引变量
- 如何使用循环为从csv导入的多个类似列创建名称相似的字符串
- 基于相似的数组键创建数组批
最新更新
- 返回一个字典,每个字符都有多个实例
- AWS S3 -仅Zip文件对象,而不是路径
- 公共api如何处理CORS起源和jwt ?
- 如何摆脱实际上"duplicates"几乎等同的调用行,实际上只切换了部分列数据?
- 如何在换行时中断文本?
- 使用流JAVA 8将map转换为特定的dto对象
- React Typescript:重构代码后无法过滤数组
- pyalgotrade退出订单事件(onExitOk)的问题
- 如何从Lucene文档中打印所有术语?
- 使用jQuery通过span类获取检查元素
- 如果数据在列表中,如何过滤请求中的数据?
- 是否可以使用javascript或python访问whatsapp消息数据库历史记录?
- 为什么在版本 5 中找不到松树脚本的"ta.sqrt"?
- 检查String是否包含List中的任何项
- Firebase Checkout Timer
- Pythonnet中的c#扩展和Linq
- 你好,这是大学学习编程的第一天
- 为什么单元测试一个图像字段使用SimpleUploadedFile创建多个图像?
- 从文件中删除空白而不是从目录中删除空白会抛出错误
- 如何调用异步函数之前的组件呈现在reactjs?
- scikit-learn版本不匹配__version__ '和' pip show scikit-learn '.<
- 雪花云数据平台哈希MD5或SHA256
- Traefik filter portainer 作为 docker 群中的"disabled container"
- Pandas数据框架,如何根据其他行的值替换部分行
- 如何安装Tensorflow和CUDA驱动程序?
- 如何创建带有域名和可导出过程列表的COBOL模块
- Pixi js对一个元素进行两次旋转(如何添加类似包装器的东西)
- 如何停止旅行计划:在Windows 10的本地计算机上运行
- r语言 - 使用for循环和数据框架从vector中提取元素
- 为什么控制台.log在我单击按钮后再次打印状态,即使状态的值没有更改?
热门标签:
javascript python java c# php android html jquery c++ css ios sql mysql arrays asp.net json python-3.x ruby-on-rails .net sql-server django objective-c excel regex ruby linux ajax iphone xml vba spring asp.net-mvc database wordpress string postgresql wpf windows xcode bash git oracle list vb.net multithreading eclipse algorithm macos powershell visual-studio image forms numpy scala function api selenium