[英]Using tapply after convert a data.frame to a data_frame (dplyr) - R
I am using the dplyr and nicheRover packages to do some calculations 我正在使用dplyr和nicheRover软件包进行一些计算
require(nicheROVER)
data(fish)
require(dplyr)
fish <- fish %>% group_by(species)
# other things I do with dplyr
clrs <- c("black", "red", "blue", "orange") # colors for each species
fish.par <- tapply(1:nrow(fish), fish$species, function(ii) niw.post(nsamples = 10,
X = fish[ii, 2:4]))
# format data for plotting function
fish.data <- tapply(1:nrow(fish), fish$species, function(ii) X = fish[ii, 2:4])
niche.plot(niche.par = fish.par, niche.data = fish.data, pfrac = 0.05,
col = clrs, xlab = expression("Isotope Ratio (per mil)"))
This gives 这给
Error in density.default(niche.data[[ii]][, ci], n = ndens) :
argument 'x' must be numeric
Then if I check with str() it results in a tbl_df 然后,如果我用str()检查,则结果为tbl_df
str(fish.data[[1]][,1])
Classes ‘tbl_df’ and 'data.frame': 69 obs. of 1 variable: $ D15N: num 12.2 11.2 12.1 11.2 12.1 ...
how can I change the tapply
command above so that the columns are num like this 我该如何更改上面的
tapply
命令,以便像这样的列数
str(fish.data[[1]][,1])
num [1:69] 12.2 11.2 12.1 11.2 12.1 ...
Thanks! 谢谢!
Try 尝试
fish.data <- tapply(1:nrow(fish), fish$species,
function(ii) X = as.data.frame(fish[ii, 2:4]))
niche.plot(niche.par = fish.par, niche.data = fish.data, pfrac = .1,
iso.names = expression(delta^{15}*N, delta^{13}*C, delta^{34}*S),
col = clrs, xlab = expression("Isotope Ratio (\u2030)"))
Note that I took the niche.plot
command straight out of the examples of niche.plot
. 请注意,我直接从
niche.plot
的示例中niche.plot
了niche.plot
命令。 Your line produced a separate and seemingly unrelated error. 您的行产生了一个单独的看似无关的错误。
It seems that niche.plot
is doing some subsetting at some point, expecting a vector or singular value as output. 似乎
niche.plot
在某个时候正在做一些子集,期望向量或奇异值作为输出。 tbl_df
's do not reduce to vectors if subsetting a single row or column though, unlike standard data.frames
, and this seems to cause the problem. 但是,如果将子行或子集设置为子集,则
tbl_df
不会减少为向量,这与标准data.frames
不同,这似乎引起了问题。 In the above code, I have just reconverted the output into a standard dataframe. 在上面的代码中,我刚刚将输出转换为标准数据帧。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.