[英]How to get an entire column from a table with multiple columns on a condition that the column contains a specific value in R
I have a simple table in R which contains 3 columns (named 'countries_A', 'countries_B', and 'countries_C') each containing 4 countries.我在 R 中有一个简单的表,其中包含 3 列(名为“countries_A”、“countries_B”和“countries_C”),每列包含 4 个国家/地区。 Now what I would like to do is write a function that searches in the table for a specific country, say "Italy", and then returns all the elements of the column where that country is in as a vector or list (with the exception of that specific country).
现在我想做的是编写一个 function 在表中搜索特定国家,比如“意大利”,然后返回该国家所在列的所有元素作为向量或列表(除了那个特定的国家)。 So in my example here, as "Italy" in the column "countries_B" along with Sweden, Spain, and Switzerland, it means I would get a single vector containing all elements of column "countries_B" except Italy, meaning I would get "[Sweden, Spain, and Switzerland]" as an answer.
因此,在我的示例中,作为“countries_B”列中的“Italy”以及瑞典、西班牙和瑞士,这意味着我将获得一个包含“countries_B”列中除意大利以外的所有元素的单个向量,这意味着我将获得“[瑞典、西班牙和瑞士]”作为答案。 I would really appreciate any help at all.
我真的很感激任何帮助。 And if possible, I would like the search to be vectorised if possible using library like dtplyr preferably.
如果可能的话,我希望尽可能使用像 dtplyr 这样的库来对搜索进行矢量化。 Below, I am attaching a screenshot of my table and the R script to generate that table.
下面,我附上了我的表格的屏幕截图和 R 脚本以生成该表格。 Many thanks in advance.
提前谢谢了。
countries <- data.frame("countries_A" =
c('Belgium','Holland', 'France', 'Germany'),
"countries_B" = c('Sweden','Italy','Spain','Switzerland'),
"countries_C"= c('England','Denmark','Portugal','Hungary'))
Let me know if this helps you:让我知道这是否对您有帮助:
find.country <- function(df, country){
df$ID <- seq.int(nrow(df))
df1 <-df %>%
pivot_longer(cols = -ID) %>%
filter(value == {country})
column.name <- df1$name
df2 <- df %>%
pivot_longer(cols = -ID) %>%
filter(name == column.name) %>%
filter(value != {country})
vector <- as.vector(df2$value)
return(vector)
}
find.country(df = countries, country = "Italy")
[1] "Sweden" "Spain" "Switzerland"
Not too elegant:不太优雅:
get_vector = function(df_countries, country){
column = countries %>% mutate(across(everything(),
~if_else(country %in% .x, cur_column(), NULL))) %>%
mutate(var = coalesce(countries_A, countries_B, countries_C)) %>%
select(var) %>% slice(1) %>% pull
vector = countries %>% select(column) %>% filter(.data[[column]] != country) %>% pull %>% as.vector
return(vector)
}
get_vector(countries, 'Sweden')
> get_vector(countries, 'Sweden')
[1] "Italy" "Spain" "Switzerland"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.