我想使用 R 在 SQL 中执行相当于更新查询的操作。如果我有这样的数据:
# Example Data
df <- data.frame(x <- c("My Name", "My Name"), y <- c("AZ", "MI"))
colnames(df) <- c("Name", "State")
print(df)
> print(df)
Name State
1 My Name AZ
2 My Name MI
我想在 SQL 中执行与此等效的 R:
update df
set Name = 'My Name1'
where Name = 'My Name'
and State = 'MI
所以最终的输出是:
> print(df)
Name State
1 My Name AZ
2 My Name1 MI
有什么想法吗?
最佳答案
因为您在数据框中使用了因子,所以您需要首先使用
levels(df$Name) <- c(levels(df$Name), "My Name1")
在实际使用更改数据框之前
df[df$Name == "My Name" & df$State == "MI", "Name"] <- "My Name1"
关于sql - R 等价于 SQL UPDATE 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25001362/