我想将用户定义的函数应用于R中数据框中每一行的特定列,并将结果保存回列中。
After nearly 3 hours of reading Stack Overflow answers about slightly different use cases and playing around with apply, lapply, sapply etc. here's the code that allows you to do this:
your_function <- function(x) {
if(x == 3) {
x = 333
} else {
x = 1
}
}
df$colname <- sapply(df$colname, your_function)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.