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

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

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"
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" 
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"
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 
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 
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
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)

  • 26
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ALittleHigh

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值