在数据框中添加一行,从向量 R 中获取值
Add a row in a dataframe taking values from a vector R
我有一个这样的df
df <- data.frame (id = c(123,123,456), w1= c("abc","fgh","kit"), w2 = c("eat","drink","ty"))
id w1 w2
1 123 abc eat
2 123 fgh drink
3 456 kit ty
和一个向量
vec <- c('value1', 'value2').
当有精确对应时,我想将这些值添加到df中。我想获得的最终df是这样的:
id w1 w2 new_col
1 123 abc eat value1
2 123 abc eat value2
3 123 fgh drink no correspondance
4 456 kit ty no correspondance
我试过这个代码
for (i in 1:length(df$id)) { ## for iterating each row
if (df$w2[i] == 'eat') {
df$new_col[i] <- vec ### how to? Here I need to replace both 'value1' and 'value2' copying the row
}
}
有人可以给我一些建议吗?提前致谢!
使用tidyr
库:
> library(tidyr)
> df[1, 'new_col'] <- toString(vec)
> df %>% separate_rows(new_col)
# A tibble: 4 x 4
id w1 w2 new_col
<dbl> <chr> <chr> <chr>
1 123 abc eat value1
2 123 abc eat value2
3 123 fgh drink <NA>
4 456 kit ty <NA>
>
编辑:
> library(tidyr)
> df[1, 'new_col'] <- toString(vec)
> df %>% %>% filter(new_col %in% c('value1', 'value2')) %>% separate_rows(new_col) %>% bind_rows(filter(df, !new_col %in% c('value1', 'value2')))
# A tibble: 4 x 4
id w1 w2 new_col
<dbl> <chr> <chr> <chr>
1 123 abc eat value1
2 123 abc eat value2
3 123 fgh drink <NA>
4 456 kit ty <NA>
>
您可以将列表列添加到数据框中,然后使用 tidyr::unnest
将它们作为单独的行。
inds <- df$w2 == "eat"
df$new_col[!inds] <- 'no correspondance'
df$new_col[inds] <- list(vec)
tidyr::unnest(df, new_col)
# id w1 w2 new_col
# <dbl> <chr> <chr> <chr>
#1 123 abc eat value1
#2 123 abc eat value2
#3 123 fgh drink no correspondance
#4 456 kit ty no correspondance
使用 tidyverse
,我们可以用 case_when
和 return 创建一个逻辑条件 list
列,然后执行 unnest
ing list
列
library(dplyr)
library(tidyr)
df %>%
mutate(new_col = case_when(w2 == 'eat' ~ list(vec),
TRUE ~ list('no correspondance'))) %>%
unnest(new_col)
# A tibble: 4 × 4
id w1 w2 new_col
<dbl> <chr> <chr> <chr>
1 123 abc eat value1
2 123 abc eat value2
3 123 fgh drink no correspondance
4 456 kit ty no correspondance
我有一个这样的df
df <- data.frame (id = c(123,123,456), w1= c("abc","fgh","kit"), w2 = c("eat","drink","ty"))
id w1 w2
1 123 abc eat
2 123 fgh drink
3 456 kit ty
和一个向量
vec <- c('value1', 'value2').
当有精确对应时,我想将这些值添加到df中。我想获得的最终df是这样的:
id w1 w2 new_col
1 123 abc eat value1
2 123 abc eat value2
3 123 fgh drink no correspondance
4 456 kit ty no correspondance
我试过这个代码
for (i in 1:length(df$id)) { ## for iterating each row
if (df$w2[i] == 'eat') {
df$new_col[i] <- vec ### how to? Here I need to replace both 'value1' and 'value2' copying the row
}
}
有人可以给我一些建议吗?提前致谢!
使用tidyr
库:
> library(tidyr)
> df[1, 'new_col'] <- toString(vec)
> df %>% separate_rows(new_col)
# A tibble: 4 x 4
id w1 w2 new_col
<dbl> <chr> <chr> <chr>
1 123 abc eat value1
2 123 abc eat value2
3 123 fgh drink <NA>
4 456 kit ty <NA>
>
编辑:
> library(tidyr)
> df[1, 'new_col'] <- toString(vec)
> df %>% %>% filter(new_col %in% c('value1', 'value2')) %>% separate_rows(new_col) %>% bind_rows(filter(df, !new_col %in% c('value1', 'value2')))
# A tibble: 4 x 4
id w1 w2 new_col
<dbl> <chr> <chr> <chr>
1 123 abc eat value1
2 123 abc eat value2
3 123 fgh drink <NA>
4 456 kit ty <NA>
>
您可以将列表列添加到数据框中,然后使用 tidyr::unnest
将它们作为单独的行。
inds <- df$w2 == "eat"
df$new_col[!inds] <- 'no correspondance'
df$new_col[inds] <- list(vec)
tidyr::unnest(df, new_col)
# id w1 w2 new_col
# <dbl> <chr> <chr> <chr>
#1 123 abc eat value1
#2 123 abc eat value2
#3 123 fgh drink no correspondance
#4 456 kit ty no correspondance
使用 tidyverse
,我们可以用 case_when
和 return 创建一个逻辑条件 list
列,然后执行 unnest
ing list
列
library(dplyr)
library(tidyr)
df %>%
mutate(new_col = case_when(w2 == 'eat' ~ list(vec),
TRUE ~ list('no correspondance'))) %>%
unnest(new_col)
# A tibble: 4 × 4
id w1 w2 new_col
<dbl> <chr> <chr> <chr>
1 123 abc eat value1
2 123 abc eat value2
3 123 fgh drink no correspondance
4 456 kit ty no correspondance