按行查找数据框中某个值的所有列
我试图在数据框中的每一行中找到具有特定数字的第一列和具有相同值的最后一列。如果数量为 4,请参阅示例数据和所需的输出。
示例数据
ID WZ_1 WZ_2 WZ_3 WZ_4
1 5 4 4 3
2 4 4 3 3
3 4 4 4 4
示例输出
ID First Last
1 WZ_2 WZ_3
2 WZ_1 WZ_2
3 WZ_1 WZ_4
I am trying to find the first column that has a specific number and the last column of the same value by each row in a dataframe. See example data and desired output if the number was 4.
Example Data
ID WZ_1 WZ_2 WZ_3 WZ_4
1 5 4 4 3
2 4 4 3 3
3 4 4 4 4
Example Output
ID First Last
1 WZ_2 WZ_3
2 WZ_1 WZ_2
3 WZ_1 WZ_4
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用
max.col
:数据
With
max.col
:data
这是一个
didyverse
选项,我在其中放入了长表格,然后filter
仅保留4
的值,并且仅保留第一个和最后的出现。然后,我创建一个新列来表示它是第一个值还是最后一个值,然后将其转移回宽格式。输出
数据
Here's a
tidyverse
option, where I put into long form, thenfilter
to keep only the values with4
and only the first and last occurrence. Then, I create a new column to denote whether it is the first or last value, then pivot back to the wide format.Output
Data