将列(长)的级别重新整理为新列(宽)(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 vianew_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_data
和ifelse()
的每一行,对应于该骑行的列中的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 inMapped
. I can get levels and use them to create a mapped DF vianew_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
andifelse()
a1
in the column that corresponds to that ride to show it had a rider, and a0
otherwise, but someone must see how to do this more elegantly? As it stands, I would need anifelse
for every column extracted from the levels in$ride
which I know has to be more verbose than required.
原文:https://stackoverflow.com/questions/38820039
最满意答案
每个进程都有自己的地址空间,这意味着虽然可以在两个进程之间共享物理内存地址,但这可能对应于每个进程的地址空间中的不同地址。 这意味着在设计将存在于共享内存中的对象时,必须确保它们使用偏移量或索引而不是纯指针。
共享内存会产生同步问题,导致问题无法解决,所以除非您真的需要,否则我建议您优先使用管道路由来共享内存。
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在哪里分配对象(Where does the JVM allocate objects)[2023-06-24]
对象在堆中分配。 然而,存储器的读取和写入当然将被缓存在处理器中。 不同的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 ...
-
在共享内存中分配对象(Allocate object in shared memory)[2022-10-26]
每个进程都有自己的地址空间,这意味着虽然可以在两个进程之间共享物理内存地址,但这可能对应于每个进程的地址空间中的不同地址。 这意味着在设计将存在于共享内存中的对象时,必须确保它们使用偏移量或索引而不是纯指针。 共享内存会产生同步问题,导致问题无法解决,所以除非您真的需要,否则我建议您优先使用管道路由来共享内存。 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 ...
-
如何测试是否删除了boost共享内存对象?(How can I test if the boost shared memory object is removed or not?)[2022-03-26]
您可以在共享内存对象中包含一个标志,指示您的服务器进程是否仍在使用它。 在服务器进程尝试删除共享内存之前,将此标志设置为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系统 ...