如何在R数据框中将一列分成多列
How to divide one column into multiple columns in R dataframe
我四处寻找答案,但还没有完全想出解决方案。
我正在尝试将我的数据框(物种计数)的多个(~60)列除以数据框中的单个列(样本工作量单位)
我能够想出以下解决方案 - 但它比我想要的更混乱。正如现在所写的那样,我可能不小心 运行 最后一行代码两次,并通过两次除法弄乱了我的值。
下面是一个简短的示例,我在其中演示了我使用的解决方案。有什么清洁剂的建议吗?
#short data.frame with some count data
#Hours is the sampling effort
counts=data.frame(sp1=sample(1:10,10),sp2=sample(1:10,10),
sp3=sample(1:10,10),sp4=sample(1:10,10),
Hours=rnorm(10,4,1))
#get my 'species' names
names=colnames(counts)[1:4]
#This seems messy: and if I run the second line twice, I will screw up my values. I want to divide all 'sp' columns by the single 'Hours' column
rates=counts
rates[names]=rates[,names]/rates[,'Hours']
p.s.: 我一直在使用 %>%,所以如果有人有解决方案,我可以只转换 'count' data.frame 而无需创建新的 data.frame,那就膨胀了!
p.s.s 我怀疑 Hadley 的功能之一可能有我需要的东西(例如 mutate_each?),但我一直没能弄清楚..
我真的看不出你的基本 R 方法有什么问题,它非常干净。如果您担心不小心 运行 第二行多次没有 运行 第一行,只需参考下面的原始 counts
列。我会像这样进行微小的调整:
rates = counts
rates[names] = counts[names] / counts[["Hours"]]
使用[
和[[
保证数据类型,而不管names
的长度。
我确实喜欢 dplyr
,但它看起来更乱:
# This works if you want everything except the Hours column
rates = counts %>% mutate_each(funs(./Hours), vars = -Hours)
# This sort of works if you want to use the names vector
rates = counts %>% mutate_at(funs(./Hours), .cols = names)
我四处寻找答案,但还没有完全想出解决方案。
我正在尝试将我的数据框(物种计数)的多个(~60)列除以数据框中的单个列(样本工作量单位)
我能够想出以下解决方案 - 但它比我想要的更混乱。正如现在所写的那样,我可能不小心 运行 最后一行代码两次,并通过两次除法弄乱了我的值。
下面是一个简短的示例,我在其中演示了我使用的解决方案。有什么清洁剂的建议吗?
#short data.frame with some count data
#Hours is the sampling effort
counts=data.frame(sp1=sample(1:10,10),sp2=sample(1:10,10),
sp3=sample(1:10,10),sp4=sample(1:10,10),
Hours=rnorm(10,4,1))
#get my 'species' names
names=colnames(counts)[1:4]
#This seems messy: and if I run the second line twice, I will screw up my values. I want to divide all 'sp' columns by the single 'Hours' column
rates=counts
rates[names]=rates[,names]/rates[,'Hours']
p.s.: 我一直在使用 %>%,所以如果有人有解决方案,我可以只转换 'count' data.frame 而无需创建新的 data.frame,那就膨胀了!
p.s.s 我怀疑 Hadley 的功能之一可能有我需要的东西(例如 mutate_each?),但我一直没能弄清楚..
我真的看不出你的基本 R 方法有什么问题,它非常干净。如果您担心不小心 运行 第二行多次没有 运行 第一行,只需参考下面的原始 counts
列。我会像这样进行微小的调整:
rates = counts
rates[names] = counts[names] / counts[["Hours"]]
使用[
和[[
保证数据类型,而不管names
的长度。
我确实喜欢 dplyr
,但它看起来更乱:
# This works if you want everything except the Hours column
rates = counts %>% mutate_each(funs(./Hours), vars = -Hours)
# This sort of works if you want to use the names vector
rates = counts %>% mutate_at(funs(./Hours), .cols = names)