创建从R中的数据帧中提取值的函数
我有一个名为创建从R中的数据帧中提取值的函数,r,function,loops,for-loop,while-loop,R,Function,Loops,For Loop,While Loop,我有一个名为data的数据框,其中包含密码的名称及其值。我还具有以下功能: for(i in 1) { x <- data$id[i] #id is the column with crypto names (such as "bitcoin") c1 <- data[data$id == x,5] #5 is the column with values } c1 #returns the value of a crypto 请告诉我如何才能做到这一点
data
的数据框,其中包含密码的名称及其值。我还具有以下功能:
for(i in 1) {
x <- data$id[i] #id is the column with crypto names (such as "bitcoin")
c1 <- data[data$id == x,5] #5 is the column with values
}
c1 #returns the value of a crypto
请告诉我如何才能做到这一点。一个简单的解决方案,包含一些玩具数据:
crypto <- data.frame(name=c("bitcoin","etherium","solana","polcadot","elrond"),value=c(50000,2500,34,27,147))
crypto_values <- function(cryptoName){
value <- crypto[crypto$name==cryptoName,"value"]
return(value)
}
crypto_values("bitcoin")
[1] 50000
加密
crypto <- data.frame(name=c("bitcoin","etherium","solana","polcadot","elrond"),value=c(50000,2500,34,27,147))
crypto_values <- function(cryptoName){
value <- crypto[crypto$name==cryptoName,"value"]
return(value)
}
crypto_values("bitcoin")
[1] 50000