简体   繁体   English

鉴于另一个向量中存在单独的列元素,R更新数据框元素

[英]R updating dataframe elements given that a separate column element exists in another vector

I have a vector vec:我有一个向量 vec:

a<-c(4,2,9,2)

And a dataframe df:和一个数据框df:

   indx explore_ind
1     1        dark
2     2        dark
3     3        dark
4     4        dark
5     5        dark
6     6        dark
7     7        dark
8     8        dark
9     9        dark
10   10        dark
    
    df<-structure(list(indx = 1:10, explore_ind = c("dark", "dark", "dark", 
"dark", "dark", "dark", "dark", "dark", "dark", "dark")), row.names = c(NA, 
10L), class = "data.frame")

I would like to update any explore_ind from "dark" to "light" where indx exists in vec.我想将任何 explore_ind 从“dark”更新为“light”,其中 indx 存在于 vec 中。

The result should be:结果应该是:

  indx explore_ind
1     1        dark
2     2        light
3     3        dark
4     4        light
5     5        dark
6     6        dark
7     7        dark
8     8        dark
9     9        light
10   10        dark

In base R在基础 R

The square brackets allows us to subset by index when we are subseting a vector, which is what we have when we use $ on a column name方括号允许我们在对向量进行子集化时按索引进行子集化,这就是我们在列名上使用$时所拥有的

df$explore_ind[a] <- "light"

If indx isn't sorted you can use如果 indx 未排序,您可以使用

df$explore_ind[df$indx %in% a] <- "light"

as suggested by @user2974951正如@user2974951 所建议的那样

ifelse option: ifelse选项:

df$explore_ind <- ifelse(df$indx %in% a, "light", "dark")
df

Output:输出:

   indx explore_ind
1     1        dark
2     2       light
3     3        dark
4     4       light
5     5        dark
6     6        dark
7     7        dark
8     8        dark
9     9       light
10   10        dark
library(tidyverse)

df<-structure(list(indx = 1:10, explore_ind = c("dark", "dark", "dark", 
                                                "dark", "dark", "dark", "dark", "dark", "dark", "dark")), row.names = c(NA, 
                                                                                                                        10L), class = "data.frame") %>% 
  as_tibble()

a<-c(4,2,9,2)

df %>%  
  mutate(explore_ind = case_when(indx %in% a ~ "light", 
                                 TRUE ~ explore_ind))
#> # A tibble: 10 x 2
#>     indx explore_ind
#>    <int> <chr>      
#>  1     1 dark       
#>  2     2 light      
#>  3     3 dark       
#>  4     4 light      
#>  5     5 dark       
#>  6     6 dark       
#>  7     7 dark       
#>  8     8 dark       
#>  9     9 light      
#> 10    10 dark

Created on 2022-06-29 by the reprex package (v2.0.1)reprex 包于 2022-06-29 创建 (v2.0.1)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 给定r中的对应向量,替换数据框中的列 - replace a column in a dataframe given a corresponding vector in r R Statistics尝试使用另一个数据帧中的元素从矢量调用元素 - R Statistics Trying to Call Element from Vector Using Elements In Another Dataframe R - 将 dataframe 列中的所有元素与另一个 dataframe 中一行中的元素进行比较,跨所有列 - R - Compare all elements in a dataframe column with an element in a row in another dataframe, across all columns R - 将向量从一个 dataframe 作为列添加到另一个 dataframe - R - Adding vector from one dataframe as column to another dataframe 有没有办法制作一个逻辑向量来查看向量的每个元素是否存在于另一个向量的所有元素中? - Is there a way to make a logical vector to see if each element of a vector exists within all of the elements of another vector? 计算dataframe列中的元素,然后在R中创建单独的列 - Count elements in dataframe column, then create separate columns in R 查找不在 R 中另一个数据框的另一列中的列中的元素 - Find elements in a column that is not in another column of another dataframe in R 检查向量R中是否存在元素 - Check if element exists in vector R 将 vector 中的元素添加到 R 中 dataframe 的每第 n 列 - Add elements from vector to every nth column of dataframe in R 替换给定 R 中另一个向量的负数位置的列? - replace a column given the position of a negative number of another vector in R?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM