R语言【taxlist】——replace_x,replace_idx,replace_na,insert_rows():数据操作

Package taxlist version 0.2.4


Description

这是一系列功能,旨在快速编码替换,既作为内部功能,也用于处理存储在向量和数据帧中的信息的工作流中。此类函数在处理存储在 taxlist 对象中的功能特征时特别有用。

replace_x() 用于替换向量中的值。

replace_idx() 通过匹配索引或条件来更改向量中的值。

函数 replace_na() 的工作方式与 replace_idx() 相同,但只会在空元素 (NA) 中插入值。

函数 insert_rows() 将同时添加行和列。当一个新表追加到另一个表但仅共享部分列时,将使用此函数。


Usage

R 复制代码
replace_x(x, old, new)

replace_idx(x, idx1 = x, idx2 = idx1, new)

replace_na(x, idx1, idx2 = idx1, new)

insert_rows(x, y)

Arguments

参数【x】:要修改的向量。在 insert_rows() 的情况下,x 是一个数据框。

参数【old】:一个向量,它的值要被replace_x() 替换。

参数【new】:一个向量,用来替换别的值,可以是数值,也可以是索引。

参数【idx1,idx2】:应用于值替换的索引,以分别将 x 与 new 匹配。如果未提供 idx2,则假定它等效于 idx1。

参数【y】:插入 x 的数据框。


Value

具有修改值的向量或数据框。


Examples

R 复制代码
replace_x(x = letters, old = c("b", "p", "f"), new = c("bee", "pork", "fungus"))
复制代码
 [1] "a"      "bee"    "c"      "d"      "e"      "fungus" "g"     
 [8] "h"      "i"      "j"      "k"      "l"      "m"      "n"     
[15] "o"      "pork"   "q"      "r"      "s"      "t"      "u"     
[22] "v"      "w"      "x"      "y"      "z"
R 复制代码
replace_idx(x = letters, idx1 = 1:length(letters), idx2 = c(2, 7, 17),
  new = c("second", "seventh", "seventeenth"))
复制代码
 [1] "a"           "second"      "c"           "d"          
 [5] "e"           "f"           "seventh"     "h"          
 [9] "i"           "j"           "k"           "l"          
[13] "m"           "n"           "o"           "p"          
[17] "seventeenth" "r"           "s"           "t"          
[21] "u"           "v"           "w"           "x"          
[25] "y"           "z" 
R 复制代码
letters[2] <- NA
replace_na(x = letters, idx1 = 1:length(letters), idx2 = c(1:3),
  new = c("alpha", "beta", "zeta"))
复制代码
 [1] "a"    "beta" "c"    "d"    "e"    "f"    "g"    "h"    "i"   
[10] "j"    "k"    "l"    "m"    "n"    "o"    "p"    "q"    "r"   
[19] "s"    "t"    "u"    "v"    "w"    "x"    "y"    "z"
R 复制代码
summary(as.factor(Easplist$life_form))
复制代码
  acropleustophyte        chamaephyte     climbing_plant 
                 8                 25                 25 
facultative_annual    obligate_annual       phanerophyte 
                20                114                 26 
  pleustohelophyte         reed_plant      reptant_plant 
                 8                 14                 19 
     tussock_plant               NA's 
                52               3576 
R 复制代码
Easplist@taxonTraits$lifeform <- replace_x(x = Easplist@taxonTraits$life_form,
  old = c("obligate_annual", "facultative_annual"), new = c("annual", "annual"))
summary(as.factor(Easplist$lifeform))
复制代码
acropleustophyte           annual      chamaephyte   climbing_plant 
               8              134               25               25 
    phanerophyte pleustohelophyte       reed_plant    reptant_plant 
              26                8               14               19 
   tussock_plant             NA's 
              52             3576 
R 复制代码
Easplist@taxonTraits$lifeform <- replace_idx(x = Easplist@taxonTraits$life_form,
  idx1 = grepl("annual", Easplist@taxonTraits$life_form), idx2 = TRUE,
  new = "annual")
summary(as.factor(Easplist$lifeform))
复制代码
acropleustophyte           annual      chamaephyte   climbing_plant 
               8              134               25               25 
    phanerophyte pleustohelophyte       reed_plant    reptant_plant 
              26                8               14               19 
   tussock_plant             NA's 
              52             3576
R 复制代码
data(iris)
iris$Species <- paste(iris$Species)
new_iris <- data.frame(Species = rep("humilis", 2), Height = c(15, 20),
  stringsAsFactors = FALSE)
insert_rows(iris, new_iris)
相关推荐
百锦再3 小时前
详细解析 .NET 依赖注入的三种生命周期模式
java·开发语言·.net·di·注入·模式·依赖
风吹落叶花飘荡3 小时前
2025 Next.js项目提前编译并在服务器
服务器·开发语言·javascript
失败又激情的man4 小时前
python之requests库解析
开发语言·爬虫·python
专注VB编程开发20年4 小时前
常见 HTTP 方法的成功状态码200,204,202,201
开发语言·网络协议·tcp/ip·http
有没有没有重复的名字4 小时前
线程安全的单例模式与读者写者问题
java·开发语言·单例模式
开开心心_Every5 小时前
便捷的电脑自动关机辅助工具
开发语言·人工智能·pdf·c#·电脑·音视频·sublime text
霖006 小时前
C++学习笔记三
运维·开发语言·c++·笔记·学习·fpga开发
上单带刀不带妹6 小时前
JavaScript中的Request详解:掌握Fetch API与XMLHttpRequest
开发语言·前端·javascript·ecmascript
小白学大数据7 小时前
Python爬取闲鱼价格趋势并可视化分析
开发语言·python
ningmengjing_7 小时前
在 PyCharm 中安装并配置 Node.js 的指南
开发语言·javascript·ecmascript