[英]comparing an element in a column in a data frame with another element in the same column in another data frame for corresponding rows in R
I am new to programming and obviously new to R. I am learning some documents in R and started programming in R. 我是编程的新手,显然是R的新手。我正在R中学习一些文档,并开始在R中进行编程。
I have four data frames with data frame names Data1
, Data2
, Data3
, Data4
. 我有四个数据帧,其数据帧名称分别为
Data1
, Data2
, Data3
, Data4
。 Each data frames has eight columns ( V1
, V2
, V3
, V4
, V5
, V6
, V7
, V8
) and 10,000 rows.The number of rows and number of columns is same for all the data frames. 每个数据帧有八列(
V1
, V2
, V3
, V4
, V5
, V6
, V7
, V8
)和10,000行。所有数据帧的行数和列数相同。
I want to compare the elements of each row's 8th column ( V8
) of all the four data frame with each other with their corresponding rows and find the maximum and minimum value. 我想将所有四个数据帧的每一行的第8列(
V8
)的元素与它们对应的行相互比较,以找到最大值和最小值。 For example if I have 10 rows and 8 columns in each data frame, I have to compare the 1st row 8th column element of Data1
, Data2
, Data3
, Data4
to find the maximum and minimum value. 例如,如果每个数据帧中有10行8列,则必须比较
Data1
, Data2
, Data3
和Data4
的第1行第8列元素,以找到最大值和最小值。 Then i have to compare the 2nd row 8th column element of Data1
, Data2
, Data3
, Data4
to find the maximum and minimum value. 然后,我必须比较
Data1
, Data2
, Data3
, Data4
的第二行第八列元素,以找到最大值和最小值。 Similary the 3rd row 8th column element, 4th row 8th column element and i have to do this for remaining 10,000 rows. 类似地,第三行第八列元素,第四行第八列元素和我必须为剩余的10,000行执行此操作。 How should I do this and what function should I use?
我应该怎么做以及应该使用什么功能?
Sample data: 样本数据:
Data1 <- as.data.frame(matrix(runif(80), 10, 8))
Data2 <- as.data.frame(matrix(runif(80), 10, 8))
Data3 <- as.data.frame(matrix(runif(80), 10, 8))
Data4 <- as.data.frame(matrix(runif(80), 10, 8))
You can do: 你可以做:
pmin(Data1$V8, Data2$V8, Data3$V8, Data4$V8)
pmax(Data1$V8, Data2$V8, Data3$V8, Data4$V8)
Or something more programmatic (there can be many variations here) 或更具编程性的内容(此处可能有很多变化)
Datas <- mget(paste0("Data", 1:4))
do.call(pmin, lapply(Datas, `[[`, "V8"))
do.call(pmax, lapply(Datas, `[[`, "V8"))
You could combine your columns in a new dataframe. 您可以将列合并到一个新的数据框中。 Then its easy to find the row-wise min or max values:
然后很容易找到按行的最小值或最大值:
newd <- data.frame(a=Data1$V8, b=Data2$V8, c=Data3$V8, d=Data4$V8)
apply(newd, 1, max)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.