能否请您将此python代码转换为R代码?非常感谢。
import pandas as pd
data = [{'A':10, 'B':100}, {'A':11,'B':110}, {'A':12,'B':120}]
df = pd.DataFrame(data)
for index, row in df.iterrows():
if row['A'] == 10:
print(row['B'])
答案 0 :(得分:1)
您可以创建list
数据,然后rbind
将其放入data.frame
。之后,您可以使用子设置。请参见下面的代码:
data <- list(c(a = 10, b = 100), c(a = 11, b = 110), c(a = 12, b = 120))
df <- as.data.frame(do.call(rbind, data))
df[df$a == 10, ]$b
输出:
[1] 100
或者您可以使用for
循环来模拟Python的方法,但是效率不及上面一种:
data <- list(c(a = 10, b = 100), c(a = 11, b = 110), c(a = 12, b = 120))
df <- as.data.frame(do.call(rbind, data))
for (index in seq.int(nrow(df)))
if (df[index, "a"] == 10)
print(df[index, "b"])
输出相同:
[1] 100