[英]Iterating through the rows a dataframe and changing the value of one column based on another
I can't seem to find an answer here. 我似乎在这里找不到答案。 I have a dataframe that has 800k rows.
我有一个具有80万行的数据框。 I want to iterate over each row, pass the value of a string in one column to the Google Translate API and then add the translation to another column in that row.
我想遍历每一行,将一列中的字符串值传递给Google Translate API,然后将翻译添加到该行的另一列中。 When I do it this way it only gives me the values from the first row for the whole dataframe (
df$word
, and df$heading
) and reproduces it in every row after. 当我这样做时,它只为我提供整个数据帧的第一行的值(
df$word
和df$heading
),并在之后的每一行中复制它。 What I want is to get the value of $word
in row 1, I want to translate it with the API and make that translation the value of $fr_translation
in row 1. I want to do this for each individual row. 我想要的是在第1行中获取
$word
的值,我想使用API对其进行转换, $fr_translation
在第1行$fr_translation
其转换为$fr_translation
的值。我想针对每一行进行此操作。 I am sure it is something easy but I am pretty new to R. Here is the code that works for the translation but not for each individual row: 我敢肯定这很容易,但是我对R还是很陌生。这是适用于翻译但不适用于每一行的代码:
library(translate)
set.key("my_key")
for(i in seq_len(nrow(df))){
df$fr_translation <- translate(df$word, 'en', 'fr')
df$fr_heading <- translate(df$heading, 'en', 'fr')
}
You are not referencing the i
th row at each iteration. 您没有在每次迭代中引用第
i
行。 Fix the part inside the loop to be like this: 将部分固定在循环中,如下所示:
df$fr_translation[i] <- translate(df$word[i], 'en', 'fr')
df$fr_heading[i] <- translate(df$heading[i], 'en', 'fr')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.