Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 在向量末尾添加字符串时遇到问题,该向量为';名单上有什么_R_List_Vector_Append - Fatal编程技术网

R 在向量末尾添加字符串时遇到问题,该向量为';名单上有什么

R 在向量末尾添加字符串时遇到问题,该向量为';名单上有什么,r,list,vector,append,R,List,Vector,Append,想知道如何在食物中添加另一种元素吗? 尝试过食物g[[“食物”]使用purr的一个选项可以是: g[["food"]] <- c(g[["food"]], "asparagus") 如果 modify_in(g, "food", ~ c(., "asparagus")) $vegetable [1] "carrot" $food [1] "steak" "eggs" "asparagus" $numbers [1] 4 2 1 7 或者使用modifyList

想知道如何在食物中添加另一种元素吗?
尝试过食物
g[[“食物”]使用
purr
的一个选项可以是:

g[["food"]] <- c(g[["food"]], "asparagus")

如果

modify_in(g, "food", ~ c(., "asparagus"))

$vegetable
[1] "carrot"

$food
[1] "steak"     "eggs"      "asparagus"

$numbers
[1] 4 2 1 7

或者使用
modifyList
from
base R

library(purrr)
map_if(g, names(g) == 'food', ~ c(.x, 'asparagus'))
#$vegetable
#[1] "carrot"

#$food
#[1] "steak"     "eggs"      "asparagus"

#$numbers
#[1] 4 2 1 7
基R解

modifyList(g, list(food = c(g[['food']], 'asparagus')))
#$vegetable
#[1] "carrot"

#$food
#[1] "steak"     "eggs"      "asparagus"

#$numbers
#[1] 4 2 1 7
g[[“食品”]]
modifyList(g, list(food = c(g[['food']], 'asparagus')))
#$vegetable
#[1] "carrot"

#$food
#[1] "steak"     "eggs"      "asparagus"

#$numbers
#[1] 4 2 1 7
g <- within(g,food <- c(food,"asparagus"))
g <- within(g,food <- append(food,"asparagus"))
> g
$vegetable
[1] "carrot"

$food
[1] "steak"     "eggs"      "asparagus"

$numbers
[1] 4 2 1 7