[英]How do I add factors to a new variable in a small dataframe in R?
Quick question: I have a small dataset that I melted and now would like to add a column variable: 快速的问题:我有一个小的数据集,我现在融化了,现在想添加一个列变量:
> df
taxa spring summer fall LocationA LocationB
1 Chironominae 1957.50 537.54 3396.765 1712.196 2958.642
2 Culicoides 863.53 343.08 2796.647 1907.804 1384.642
3 Naididae 949.88 40.75 147.569 641.911 91.566
4 Asioplax 1799.41 163.04 119.882 0.000 1343.528
5 Nematoda 0.00 166.00 27.647 53.679 45.057
6 Stilobezzia 0.00 20.38 885.961 605.179 222.321
taxa <- melt(df)
> taxa
taxa variable value
1 Chironominae spring 1957.500
2 Culicoides spring 863.530
3 Naididae spring 949.880 .....
How do I add a column for "variable.type" that defines whether the "variable" is a season (rows 1:18) or a location (rows 19:30)? 如何为“ variable.type”添加一列,以定义“变量”是季节 (行1:18)还是位置 (行19:30)? I tried: 我试过了:
taxa$variable.type <- c("Season"[1:18], "Location"[19:30])
but just ended up with a column with lots of NAs. 但是刚结束时有很多NA的专栏。
thanks! 谢谢!
尝试:
df$variable.type <- ifelse(df$variable %in% c("spring","summer","fall"),"season","location")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.