首页 \ 问答 \ 将列(长)的级别重新整理为新列(宽)(Reshape levels of a column (long) into new columns (wide))

将列(长)的级别重新整理为新列(宽)(Reshape levels of a column (long) into new columns (wide))

我想在一个DF中获取一列的级别,并将每个级别添加为新DF中的新列。 这是一个玩具数据集,显示源和理想目标DF。

来源DF

person  hour  ride 
Bill     1      A
Sue      2      B
Bob      1      C
Jill     3      B
Dan      3      A
Tina     3      A

映射DF

hour   A   B   C   Saturation 
1      1   0   1     .66
2      0   1   0     .33
3      1   1   0     .66

这是一个测试数据集:

test_data <- cbind.data.frame(person = c('Bill', 'Sue', 'Bob', 'Jill', 'Dan', 'Tina'),
                              hour = factor(c(1, 2, 1, 3, 3, 3)),
                              ride = c('A', 'B', 'C', 'B', 'A', 'A'))

test_data$person <- as.character(test_data$person)

了解Source每次骑行如何变成Mapped的新列。 我可以获得级别并使用它们来创建映射的DF via

new_data <- cbind.data.frame(hour = levels(test_data$hour))

但是当我尝试迭代级别来添加新列时,它都失败了。 我看到了水平。

unlist(lapply(levels(test_data$ride), function(x) paste(x)))

产量

[1] "A" "B" "C"

那么如何通过$ride的级别并在映射的DF中添加一列?

奖励:我将遍历test_dataifelse()的每一行,对应于该骑行的列中的1表示它有骑手,否则为0 ,但有人必须看到如何更优雅地执行此操作? 就目前而言,我需要从$ride的级别中提取的每个列都有一个ifelse ,我知道它必须比需要的更冗长。


I want to take the levels of a column in one DF and add each level as a new column in a new DF. Here is a toy dataset showing the source and ideal target DFs.

Source DF

person  hour  ride 
Bill     1      A
Sue      2      B
Bob      1      C
Jill     3      B
Dan      3      A
Tina     3      A

Mapped DF

hour   A   B   C   Saturation 
1      1   0   1     .66
2      0   1   0     .33
3      1   1   0     .66

Here is a test data set:

test_data <- cbind.data.frame(person = c('Bill', 'Sue', 'Bob', 'Jill', 'Dan', 'Tina'),
                              hour = factor(c(1, 2, 1, 3, 3, 3)),
                              ride = c('A', 'B', 'C', 'B', 'A', 'A'))

test_data$person <- as.character(test_data$person)

See how each ride in Source turns into a new column in Mapped. I can get levels and use them to create a mapped DF via

new_data <- cbind.data.frame(hour = levels(test_data$hour))

but it all fails when I try to iterate through levels to add new columns. I see the levels.

unlist(lapply(levels(test_data$ride), function(x) paste(x)))

yields

[1] "A" "B" "C"

So how to go through the levels in $ride and add a column in the mapped DF?

Bonus: I am going to run through each of the rows in test_data and ifelse() a 1 in the column that corresponds to that ride to show it had a rider, and a 0 otherwise, but someone must see how to do this more elegantly? As it stands, I would need an ifelse for every column extracted from the levels in $ride which I know has to be more verbose than required.


原文:https://stackoverflow.com/questions/38820039
更新时间:2023-05-26 20:05

最满意答案

每个进程都有自己的地址空间,这意味着虽然可以在两个进程之间共享物理内存地址,但这可能对应于每个进程的地址空间中的不同地址。 这意味着在设计将存在于共享内存中的对象时,必须确保它们使用偏移量或索引而不是纯指针。

共享内存会产生同步问题,导致问题无法解决,所以除非您真的需要,否则我建议您优先使用管道路由来共享内存。


Each process has its own address space which means that although a physical memory address may be shared between the two processes, this probably will correspond to different addresses in each process's address space. This means that when designing object that will exist in shared memory you MUST ensure they use offsets or indexes and not pure pointers.

Sharing memory will create synchronisation issues which can cause no end of problems, so unless you really have to, I will advise you to use the pipe route in preference to shared memory.

相关问答

更多
  • 对象在堆中分配。 然而,存储器的读取和写入当然将被缓存在处理器中。 不同的JVM会做不同的事情,但大多数会有一个线程本地分配缓冲区,这意味着不同的线程将在不同的分区中分配对象; 垃圾收集器然后将回收这些并且(如果对象仍然存活)在必要时将它们移动到堆存储器的不同区域。 Objects are allocated in the heap. However reads and writes of memory will of course be cached in the processor. Different ...
  • 我想使用相同的矢量类型,但不是在共享内存中分配它 我们就在那里停下来。 它不再是同一个向量。 鉴于分配器是向量的模板参数,不同的分配器意味着不同的类型。 就像说std::vector和std :: vector甚至不是同一类型一样简单。 我是否必须更改向量的定义才能接受这两种实现? 是的,您可以使用别名声明来指定IntVector是参数化分配器的int的向量。 template using IntVector=vector; ...
  • 同样的问题在这里 这似乎与MediaWiki 1.21有关。 我将Wiki从1.20升级到1.21而不更改任何已安装的软件包,并且也得到此错误。 rsvg和ImageMagick都不再生成图像缩略图。 如果我回到1.20,事情就会重新开始。 更新 所以我试图解决这个问题,唯一有用的是降级到MediaWiki 1.20.6,其中图像转换为SVG图像的其他大小或格式适用于所有支持的转换器。 如果在升级之前没有数据库备份,则可以使用Special:Export转储所有页面,并将其导入空安装。 Same issue ...
  • 每个进程都有自己的地址空间,这意味着虽然可以在两个进程之间共享物理内存地址,但这可能对应于每个进程的地址空间中的不同地址。 这意味着在设计将存在于共享内存中的对象时,必须确保它们使用偏移量或索引而不是纯指针。 共享内存会产生同步问题,导致问题无法解决,所以除非您真的需要,否则我建议您优先使用管道路由来共享内存。 Each process has its own address space which means that although a physical memory address may be s ...
  • 我想最简单的选择是使用内存映射文件,这是Neil已经提出的。 如果这个选项填不好,另一种方法是可以定义专用的分配器。 这里有一篇关于它的好文章: 在共享内存中创建STL容器 还有优秀的IonGaztañaga的Boost.Interprocess库,带有shared_memory_object和相关功能。 Ion已经向未来TR的C ++标准化委员会提出了解决方案: 内存映射文件和共享内存对于C ++来说,这可能意味着值得考虑的解决方案。 I suppose the easiest option would ...
  • 您可以在共享内存对象中包含一个标志,指示您的服务器进程是否仍在使用它。 在服务器进程尝试删除共享内存之前,将此标志设置为false。 当客户端进程看到该标志为false时,它们可以关闭对该对象的引用。 此外,我不认为服务器进程将被允许重新创建具有相同名称的共享对象,直到它被删除,因为我确信这些名称必须是唯一的。 You could include a flag in your shared memory object that indicates if your server process is stil ...
  • __shared__.Array2D在内核中使用__shared__.Array2D来定义编译时固定大小的2D数组。 它不能在内核之外使用。 以下是一些示例: https : //github.com/quantalea/AleaGPUTutorial/blob/master/src/csharp/examples/matrix_multiplication/MatrixMult.cs#L52 __shared__.Array2D is used in kernel to define compile-ti ...
  • 当然,你已经使用pthread_barrier_wait()走上了正确的轨道。 将屏障初始化为16,然后让线程函数执行: /* ...Work on estimated memory size... */ if (pthread_barrier_wait(&barrier) == PTHREAD_BARRIER_SERIAL_THREAD) { /* ...Allocate the shared memory... */ } pthread_barrier_wait(&barrier); /* ...
  • 那么有多少份内存? 1或n + 1(其中n是进程计数) 共享内存只有一个副本。 相同的物理内存映射到不同的进程。 但它可能会映射到不同的地址。 进程是否适合通过其fd直接读/写共享内存对象? 是的。 也就是说,实际上是共享内存的目的。 一个进程写入共享内存的内容可以由另一个进程读取。 这是一种非常快速的IPC形式。 但是你必须要小心如何使用它。 特别是,您需要担心并发访问和共享内存中的共享指针。 Then how many copy of memory there are? 1 or n+1 (where ...
  • 阅读shm_overview(7) & shm_open(3) 。 如果shm_open 没有给出 O_CREAT标志,那么如果名称不存在则会失败,就像open(2)那样。 如果给出了O_CREAT 和 O_EXCL ,如果名称存在,它也将失败。 在shm_open(3)中可以说失败可能发生在: ENOENT尝试shm_open()一个不存在的名称,并且未指定O_CREAT。 EEXIST O_CREAT和O_EXCL都指定给shm_open(),并且name指定的共享内存对象已存在。 在某些Linux系统 ...

相关文章

更多

最新问答

更多
  • 如何检索Ember.js模型的所有属性(How to retrieve all properties of an Ember.js model)
  • maven中snapshot快照库和release发布库的区别和作用
  • arraylist中的搜索元素(Search element in arraylist)
  • 从mysli_fetch_array中获取选定的值并输出(Get selected value from mysli_fetch_array and output)
  • Windows Phone上的可用共享扩展(Available Share Extensions on Windows Phone)
  • 如何在命令提示符下将日期设置为文件名(How to set file name as date in command prompt)
  • 如何在Laravel 5.2中使用paginate与关系?(How to use paginate with relationships in Laravel 5.2?)
  • 从iframe访问父页面的id元素(accessing id element of parent page from iframe)
  • linux的常用命令干什么用的
  • Feign Client + Eureka POST请求正文(Feign Client + Eureka POST request body)
  • 怎么删除禁用RHEL/CentOS 7上不需要的服务
  • 为什么Gradle运行测试两次?(Why does Gradle run tests twice?)
  • 由于有四个新控制器,Auth刀片是否有任何变化?(Are there any changes in Auth blades due to four new controllers?)
  • 如何交换返回集中的行?(How to swap rows in a return set?)
  • 在android中的活动之间切换?(Switching between activities in android?)
  • Perforce:如何从Depot到Workspace丢失文件?(Perforce: how to get missing file from Depot to Workspace?)
  • Webform页面避免运行服务器(Webform page avoiding runat server)
  • 在ios 7中的UITableView部分周围绘制边界线(draw borderline around UITableView section in ios 7)
  • 内存布局破解(memory layout hack)
  • 使用Boost.Spirit Qi和Lex时的空白队长(Whitespace skipper when using Boost.Spirit Qi and Lex)
  • 我们可以有一个调度程序,你可以异步添加东西,但会同步按顺序执行吗?(Can we have a dispatcher that you can add things todo asynchronously but will be executed in that order synchronously?)
  • “FROM a,b”和“FROM a FULL OUTER JOIN b”之间有什么区别?(What is the difference between “FROM a, b” and “FROM a FULL OUTER JOIN b”?)
  • Java中的不可变类(Immutable class in Java)
  • bat批处理文件结果导出到txt
  • WordPress发布查询(WordPress post query)
  • 如何在关系数据库中存储与IPv6兼容的地址(How to store IPv6-compatible address in a relational database)
  • 是否可以检查对象值的条件并返回密钥?(Is it possible to check the condition of a value of an object and JUST return the key?)
  • 德州新起点计算机培训学校主要课程有什么?
  • GEP分段错误LLVM C ++ API(GEP segmentation fault LLVM C++ API)
  • “latin1_german1_ci”整理来自哪里?(Where is “latin1_german1_ci” collation coming from?)