[英]Python to R Translation
我在 Python 中有幾行代碼,我試圖在 R 中復制它們,但我承認我目前還不夠熟練,無法弄清楚。
這是 Python 中的代碼:
import pandas as pd
df = pd.DataGram ({'col_a' : ["blue shovel 1024", "red shovel 1022", "green bucket 3021", "green rake 3021",
"yellow shovel 1023"], 'col_b' : ["blue", "red", "green", "blue", "yellow"]},
columns = ["col_a", "col_b"])
unique_words = list(df.col_b.unique())
unique
["blue", "red", "green", "yellow"]
df['result] = df['col_a'].apply(lambda x:','.join([item for item in str(x).split () \
if item in unique_words]))
運行上述代碼的結果為您提供:
col_a col_b result
1 blue shovel 1024 blue blue
2 red shovel 1022 red red
3 green buckets 3021 green green
4 green rake 3021 blue green
5 yellow shovel 1023 yellow yellow
這段代碼的目標是在 col_b 中創建一個唯一值列表,然后在 col_a 中搜索這些唯一值中的任何一個,如果找到它們,將它們放在結果列中。 請注意,在第 4 行中,結果為綠色。 這是正確的,因為即使 col_b 顯示第 4 行的值是藍色的,col_a 中的實際值也是綠色的。
我試過重寫這個部分:
df['result] = df['col_a'].apply(lambda x:','.join([item for item in str(x).split () \
if item in unique_words]))
在 R 中(我的想法是寫一個 function 並嘗試一個 lapply(),但要么我做錯了,要么這不是正確的方法。提前謝謝你的任何建議或幫助,我會回來看看如果有任何問題我可以回答或信息我可以幫助澄清。再次感謝您!
library(tidyverse)
df <- tibble(
col_a = c("blue shovel 1024", "red shovel 1022", "green bucket 3021", "green rake 3021", "yellow shovel 1023"),
col_b = c("blue", "red", "green", "blue", "yellow")
)
df
#> # A tibble: 5 x 2
#> col_a col_b
#> <chr> <chr>
#> 1 blue shovel 1024 blue
#> 2 red shovel 1022 red
#> 3 green bucket 3021 green
#> 4 green rake 3021 blue
#> 5 yellow shovel 1023 yellow
unique_words <- unique(df$col_b)
unique_words
#> [1] "blue" "red" "green" "yellow"
unique_words_regex <- unique_words %>% paste0(collapse = "|")
df <- mutate(df, result = col_a %>% str_extract(unique_words_regex))
df
#> # A tibble: 5 x 3
#> col_a col_b result
#> <chr> <chr> <chr>
#> 1 blue shovel 1024 blue blue
#> 2 red shovel 1022 red red
#> 3 green bucket 3021 green green
#> 4 green rake 3021 blue green
#> 5 yellow shovel 1023 yellow yellow
由代表 package (v2.0.1) 於 2021 年 12 月 15 日創建
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.