如何将功能应用于R中的每一行数据帧?
我想将给定函数应用于每一行数据框,并使用其他行值,作为输入参数/参数:
Model <- c("H5", "H5", "H5","H4")
Length <- c(6, 6, 6, 6)
Code <- c("030299", "010121","030448","030324")
df <- data.frame(Model,Length,Code)
Model Length Code
HS5 6 030299
HS5 6 010121
HS5 6 030448
HS4 6 030324
我想将以下代码应用于每一行,并将结果作为新列
Library(concordance)
concord(sourcevar = (each row of 'Code' column), origin = as.character(character in 'Model' column) , destination = "HS4", dest.digit = as.numeric(number in 'Length' column), all = F))
文档第6页
I want to apply given function to each row of dataframe and use another values of row, as input parameters/arguments:
Model <- c("H5", "H5", "H5","H4")
Length <- c(6, 6, 6, 6)
Code <- c("030299", "010121","030448","030324")
df <- data.frame(Model,Length,Code)
Model Length Code
HS5 6 030299
HS5 6 010121
HS5 6 030448
HS4 6 030324
I want to apply the following code to each row and generate the outcome as a new column
Library(concordance)
concord(sourcevar = (each row of 'Code' column), origin = as.character(character in 'Model' column) , destination = "HS4", dest.digit = as.numeric(number in 'Length' column), all = F))
Documentation Page 6
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以在行上使用
应用
(margin = 1
)。但是,这是行不通的,因为“ HS4”和“ HS4”之间没有转换字典,因此您只能在不是HS4:的行上使用
应用
You can use
apply
on the rows (MARGIN = 1
).However, this does not work because there is no conversion dictionary between "HS4" and "HS4", so you can use
apply
only on the rows that are not HS4: