getDouble <- function(num=1){ result <- num * 2 #函数主体 return(result) #返回参数用return() } myResult <- getDouble(num=20) #调用函数,myResult应为40 1. 2. 3. 4. 5. 6. 7. 8. return()和print()的区别就在与print()会在Console显示输出而return()不会。 data.frame 数据框 比...
GetChunks=function(total, chunk_size=20){ n=ceiling(total/chunk_size) n1=(0:(n-1) )*chunk_size +1; n2=c(1:(n-1)*chunk_size, min(n*chunk_size, total) ) return(data.frame( n1=n1, n2=n2 )) } # test #chunks=GetChunks(10, 3) # 设置并行最多用几个核 plan(multisession, ...
当到达return()语句或到达函数主体中的最终表达式时,控制权将返回给调用函数。 如果没有显式数据返回到调用函数,则将返回最后一个表达式生成的输出。 让我们看一个简单的R函数的例子。 我们想发现我们经常在编写代码来确定通过课程的学生人数,因此我们决定定义一个函数来简化我们的代码。 该函数是使用function()表达式...
#修改并展示整体流程wilcox_data <- data.frame(Genesymbol=colnames(df)[2:ncol(df)])for(iin2:ncol(df)){print(i)wilcox_data[i-1,2] <- wilcox.test(df[,i] ~ group,data = df,exact = FALSE)[["p.value"]]}wilcox_data$fdr <...
Function body:函数体,包含一组语句,用于定义函数的作用,或者实现某种功能 return:评估/计算的函数体中的最后一个表达式 ## ## 示例1 无参函数 ## ## 示例2 带参数的函数 vector = 1:100 ## ## 示例3带默认值参数的函数 二、内置函数。 可以直接使用!
conver_counts<-function(x){q<-quantile(x)sect1<-which(q[1]<=x&x<=q[2])sect2<-which(q[2]<x&x<=q[3])sect3<-which(q[3]<x&x<=q[4])sect4<-which(q[4]<x&x<=q[5])x[sect1]<-1x[sect2]<-2x[sect3]<-3x[sect4]<-4return(x)}train<-apply(train,2,conver_counts)...
大多数R对象都是基于S3类(来源于第三代S语言),例如直方图函数hist()输出是一个包含多个组件的列表,它还有一个属性(attribute),用来指定列表的类,即histogram类。R的面向对象编程是基于泛型函数(generic function)的,而不是基于类层次结构。 类用在泛型函数中,泛型函数是一个函数族,其中的每个函数都有相似的功能,...
data_dir#直接一次性读取全部目标路径 merge<- Read10X(data.dir = data_dir)#直接将多个样品统一读取为一个稀疏矩阵 seurat_object<- CreateSeuratObject(counts = merge, min.cells = 5, min.features = 300) 这样就merge成功啦,甚至更简单! #添加分组信息: ...
The get function can also be used to call a column from a data frame. Let’s first create some example data: data<-data.frame(var1=c(5,5,5,5,5),# Create example data.framevar2=c(4,2,2,1,8)) In order to use the get function for the variables of this data frame, we first...
As a final note: In this tutorial we have returned a single value from our user-defined function. However, it would also be possible to use a similar R syntax to return other types of data objects such as a data frame, a tibble, a ggplot2 plot object, and so on…...