Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/76.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2008/2.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
使用dataframe按键重命名列表元素_R_List_Dataframe_Key_Names - Fatal编程技术网

使用dataframe按键重命名列表元素

使用dataframe按键重命名列表元素,r,list,dataframe,key,names,R,List,Dataframe,Key,Names,我有一个如下列表: $fec9 [1] "yes" $`39c1` [1] "no" $d387 [1] "yes" $`0065` [1] "yes" 以及具有与列表元素匹配的键的数据帧,如下所示: dataframe <- data.frame(key = c('39c1', 'fec9', 'p731' '0065', 'd387'), label

我有一个如下列表:

$fec9
[1] "yes"

$`39c1`
[1] "no"

$d387
[1] "yes"

$`0065`
[1] "yes"
以及具有与列表元素匹配的键的数据帧,如下所示:

dataframe <- data.frame(key = c('39c1', 'fec9', 'p731' '0065', 'd387'),
                        label = c('trash', 'wash car', 'cook dinner', 'mow lawn', 'vacuum'))

dataframe假设data.frame的列是
字符
,然后我们得到'list'和'key'的
名称
元素的
相交
并使用该元素为
列表
值分配相应的'label'

nm1 <- intersect(names(list), dataframe$key)
list[nm1] <- dataframe$label[dataframe$key %in% nm1]

nm1我希望您正在寻找:

#method 1
#get common key from dataframe
df <- df[df$key %in% names(list), ]
list <- list[df$key] #getting the same order as of dataframe
names(list) <- df$label

#method 2
#if you want to preserve the order of labels: 
df <- df[df$key %in% names(list), ]
row.names(df) <- df$key
names(list) <- df[names(list), ]$label

下面是一个使用
merge
+
stack
+
setNames

with(
  merge(stack(lst), df, by.x = "ind", by.y = "key"),
  setNames(as.list(values), label)
)

$`mow lawn`
[1] "yes"

$trash
[1] "no"

$vacuum
[1] "yes"

$`wash car`
[1] "yes"
数据

list <- list(fec9 = 'yes', `39c1` = 'no', 'd387' = 'yes', `0065` = 'yes')
df <- data.frame(key = c('39c1', 'fec9', 'p731', '0065', 'd387'),  
                        label = c('trash', 'wash car', 'cook dinner', 'mow lawn', 'vacuum'), stringsAsFactors = FALSE)
> dput(lst)
list(fec9 = "yes", `39c1` = "no", d387 = "yes", `0065` = "yes")

> dput(df)
structure(list(key = c("39c1", "fec9", "p731", "0065", "d387"
), label = c("trash", "wash car", "cook dinner", "mow lawn",
"vacuum")), class = "data.frame", row.names = c(NA, -5L))
with(
  merge(stack(lst), df, by.x = "ind", by.y = "key"),
  setNames(as.list(values), label)
)
$`mow lawn`
[1] "yes"

$trash
[1] "no"

$vacuum
[1] "yes"

$`wash car`
[1] "yes"
> dput(lst)
list(fec9 = "yes", `39c1` = "no", d387 = "yes", `0065` = "yes")

> dput(df)
structure(list(key = c("39c1", "fec9", "p731", "0065", "d387"
), label = c("trash", "wash car", "cook dinner", "mow lawn",
"vacuum")), class = "data.frame", row.names = c(NA, -5L))