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 天前
代码展示:PHP搭建海外版外卖系统源码解析
java·开发语言·前端·后端·系统架构·uni-app·php
feifeigo1233 天前
matlab画图工具
开发语言·matlab
dustcell.3 天前
haproxy七层代理
java·开发语言·前端
norlan_jame3 天前
C-PHY与D-PHY差异
c语言·开发语言
多恩Stone3 天前
【C++入门扫盲1】C++ 与 Python:类型、编译器/解释器与 CPU 的关系
开发语言·c++·人工智能·python·算法·3d·aigc
QQ4022054963 天前
Python+django+vue3预制菜半成品配菜平台
开发语言·python·django
遥遥江上月3 天前
Node.js + Stagehand + Python 部署
开发语言·python·node.js
m0_531237173 天前
C语言-数组练习进阶
c语言·开发语言·算法
Railshiqian3 天前
给android源码下的模拟器添加两个后排屏的修改
android·开发语言·javascript
雪人不是菜鸡3 天前
简单工厂模式
开发语言·算法·c#