There is a previous question here有很多相同的问题,但我不理解给出的解决方案,只是在评论中没有给出答案。因此需要一个新问题。
我已按照this guide创建了一个新的R包。
要创建,安装和加载它,我使用了以下代码:
error_reporting(E_ALL);
ini_set('display_errors', 1);
我在R文件夹中放了一个文件#libs
ibrary(pacman)
p_load(devtools, roxygen2)
#create
#navigate to the current folder
create("kirkegaard")
#make documentation
setwd("./kirkegaard")
document()
#install
setwd("..")
install("kirkegaard")
#load
library(kirkegaard)
。调用它的原因是我想将所有函数放在一个文件中,而不是像指南所说的那样将文件分开。
它的内容如下:
functions.R
该函数按行划分两个data.frames。此函数与data.frames上的完全外连接sql命令基本相同,使用rownames匹配行。
它加载正常并安装好。它在RStudio中显示为一个包。文档也在那里。但是,调用该函数只会给出:
# This file contains various useful functions for working with datasets
#
#
#this is the dataset merger
#note thet variables from DF1 are put in front
#note also that DF1 overwites any values from DF1
#' Dataset merger function
#'
#' This function allows you to merge two data.frames by their overlapping rownames.
#' @param DF1 the first data.frame
#' @param DF2 the second data.frame
#' @param main which data.frame should be used as the main? Choose the larger one if working with large datasets. Default to using neither.
#' @keywords merging combining datasets data.frame
#' @export
#' @examples
#' merge.datasets()
merge.datasets = function (DF1, DF2, main=0, time=F){
#time if desired
if (time) {time1 = proc.time()} #start timer
#colnames, remove duplicates
total.colnames = c(colnames(DF1),colnames(DF2))
total.colnames.unique = unique(total.colnames)
#rownames, remove duplicates
total.rownames = c(rownames(DF1),rownames(DF2))
total.rownames.unique = unique(total.rownames)
#combined dataset
#main setting decides how to combine
#default is to create a new DF and add everything into it
#but this will be slow for larger DFs
if (!(main == 1 | main == 2 | main == 0)){ #check for valid input
print("Valid input to parameter 'main' not provided");return(NULL)
}
if (main==0){ #create a combined dataset
DF3 = as.data.frame(matrix(nrow = length(total.rownames.unique),
ncol = length(total.colnames.unique)))
rownames(DF3) = sort(total.rownames.unique)
colnames(DF3) = total.colnames.unique
}
if (main==1){ #use first DF as main
DF3 = DF1
}
if (main==2){ #use second DF as main
DF3 = DF2
}
if (main!=2){
#loop over input dataset 2
for (variable in 1:length(colnames(DF2))){ #loop over variables/cols
for (case in 1:length(rownames(DF2))){ #loop over cases/rows
if (is.na(DF2[case,variable])){ #skip if datapoint is missing
next
}
DF3[rownames(DF2)[case], colnames(DF2)[variable]] = DF2[case,variable]
#print(DF3[rownames(DF2)[case], colnames(DF2)[variable]]) #used for debugging
}
}
}
if (main!=1){ #if DF2 is main
#loop over input dataset 1
for (variable in 1:length(colnames(DF1))){ #loop over variables/cols
for (case in 1:length(rownames(DF1))){ #loop over cases/rows
if (is.na(DF1[case,variable])){ #skip if datapoint is missing
next
}
DF3[rownames(DF1)[case], colnames(DF1)[variable]] = DF1[case,variable]
#print(DF3[rownames(DF1)[case], colnames(DF1)[variable]]) #used for debugging
}
}
}
#output time
if (time) {
time2 = proc.time()-time1 #end timer
print(time2) #print time
}
return(DF3)
}
然而,这只是给出:
#some data to merge
d1 = iris[sample(1:150, 50),] #random sample from isis
d2 = iris[sample(1:150, 50),]
#try it
merge.datasets(d1, d2)
kirkegaard::merge.datasets(d1, d2)
有什么问题? R以某种方式加载了文档,但没有加载函数。或者它隐藏在错误的命名空间中。
我查看了另一个问题的评论中提到的命名空间文件,但它没有说对我有用:
> merge.datasets(d1, d2)
Error: could not find function "merge.datasets"
> kirkegaard::merge.datasets(d1, d2)
Error: 'merge.datasets' is not an exported object from 'namespace:kirkegaard'
答案 0 :(得分:1)
基于nicola上面的评论。这里的解决方案是避免在函数名称中使用点。所以将函数重命名为merge_datasets(),然后就可以了。