如何在R中的数据框中添加列
我已将文件中的数据导入到 R 中的数据框中。它是这样的。
Name Count Category
A 100 Cat1
C 10 Cat2
D 40 Cat1
E 30 Cat3
H 3 Cat3
Z 20 Cat2
M 50 Cat10
所以现在我想根据名称列中的值添加类别列。例如,如果 Name = (A, D)、Category = 'Cat1' 等。
这只是我给出的一个简单示例。我有大量的名称和类别,因此我需要紧凑的语法。我该怎么做?
编辑:我更改了示例以更好地满足我的需求,因为名称可以是任何非数字的内容。抱歉之前没说得太清楚。
I have imported data from a file into a data frame in R. It is something like this.
Name Count Category
A 100 Cat1
C 10 Cat2
D 40 Cat1
E 30 Cat3
H 3 Cat3
Z 20 Cat2
M 50 Cat10
So now i want to add the Category column depending on the values in the column Name. So something like if Name = (A, D), Category = 'Cat1' etc.
This is only a simple example I am giving. I have a large number of Names and Categories so I want a compact syntax. How can I do this?
Edit: I've changed the example to better suit my needs as the name can be anything not numeric. Sorry for not being too clear before.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可以使用
ifelse
。如果你的数据框被称为df
,你会这样做:You can use
ifelse
. If your data frame were calleddf
you would do:您可以使用地图。 (更新为使用
stringsAsFactors = FALSE
)You can use a map. (UPDATED to use
stringsAsFactors = FALSE
)[根据OP的评论进行更新并更改Q]
使用上述数据框,我们可以进行数据库合并。您需要为您想要的
Name
Category
组合设置lookup
,如果没有大量的< code>Name(至少您只需要在lookup
中将它们分别列出一次,并且不必按顺序执行 - 列出所有Cat1
名称
优先,等等):一个选项是索引:
另一种可扩展性更好的方法是,
[Update following the OP's comment and altered Q]
Using the above data frames, we can do a data base merge. You need to set-up
lookup
for theName
Category
combinations you want, which is OK if there aren't a very large number ofName
s (At least you only need to list them once each inlookup
and you don't have to do it in order - list allCat1
Name
s first, etc):One option is indexing:
An alternative that scales better is,
查看:
cut()
recode()
car
包中的check out:
cut()
recode()
in thecar
package使用 ifelse 和 %in% 也许更简单、更易读:
Perhaps simpler and more readable using ifelse and %in%: