计算 R DataFrame 列之间的差异
通常,可以从包含一些数字数据的数据帧计算两列之间的差异。在本文中,我们将讨论如何在 R 编程语言中计算列之间的差异。
方法
- 创建一个数据框,列应该是数字或整数数据类型,以便我们可以找到它们之间的差异。
- 使用$运算符从列中提取所需的数据到单独的变量中。例如,我们有两列,然后将各个列提取到单独的变量中。
- 然后对这些列之间的差异执行减号操作。
- 最后,打印结果。
示例 1:
R
# creating a dataframe
df=data.frame(num=c(1,2,3,4),num1=c(5,4,3,2))
# Extracting column 1
a=df$num
# Extractiong column 2
b=df$num1
# printing dataframe
print(df)
# printing difference among
# two columns
print(b-a)
R
# creating a dataframe with some
# numeric data
df=data.frame(num=c(1.9,2.9,3.4,5.6,9.8),
num1=c(6.3,7.7,8.0,9.3,10.9))
print(df)
# extracting column 1 into a
# variable called a
a=df$num
# ecteacting column 2 into a
# variable called b
b=df$num1
# printing the difference between
# two columns
print(b-a)
R
# creating a dataframe with
# some numeric data
df=data.frame(num=c(1,2,3,4,5),
num1=c(6,7,8,9,10))
# extracting column 1 into a
# variable called a
a=df$num
# extracting column 2 into a
# variable called b
b=df$num1
# printing the dataframe
print(df)
# printing the difference
# between two columns
print(b-a)
输出 :
示例 2:
电阻
# creating a dataframe with some
# numeric data
df=data.frame(num=c(1.9,2.9,3.4,5.6,9.8),
num1=c(6.3,7.7,8.0,9.3,10.9))
print(df)
# extracting column 1 into a
# variable called a
a=df$num
# ecteacting column 2 into a
# variable called b
b=df$num1
# printing the difference between
# two columns
print(b-a)
输出 :
示例 3:
电阻
# creating a dataframe with
# some numeric data
df=data.frame(num=c(1,2,3,4,5),
num1=c(6,7,8,9,10))
# extracting column 1 into a
# variable called a
a=df$num
# extracting column 2 into a
# variable called b
b=df$num1
# printing the dataframe
print(df)
# printing the difference
# between two columns
print(b-a)
输出 :