简体   繁体   English

比较数据框中某列中的元素与另一数据框中同一列中的另一个元素以获取R中的对应行

[英]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 . 我有四个数据帧,其数据帧名称分别为Data1Data2Data3Data4 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. 每个数据帧有八列( V1V2V3V4V5V6V7V8 )和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列,则必须比较Data1Data2Data3Data4的第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. 然后,我必须比较Data1Data2Data3Data4的第二行第八列元素,以找到最大值和最小值。 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.

相关问题 将一个数据框的列与 R 中的另一个数据框列映射(映射从父元素创建的子元素) - map column of one data frame with another data frame column in R (map a child element which is created from parent) R-如何将数据框的列添加为另一个数据框的列? - R - How to add column of data frame as column of another data frame? R 在另一个数据框中逐列选择数据框中的列 - R Selecting column in a data frame by column in another data frame 如果另一个列值与 R 中另一个数据框中的相应单元格不匹配,则将数据框中的列值替换为 null - Replacing column values with null in a data frame if another column value doesn't match with the corresponding cell in another data frame in R 基于另一列中的值对 R 数据帧中的行进行矢量化重新编码 - Vectorized recoding of rows in R data frame based on value in another column R:根据来自另一个数据框的匹配行更新列 - R: Update column based on matching rows from another data frame 如何为另一个数据框中的元素指定列名 - How to assign a column name to an element in another data frame 如果第一列中的元素与另一个data.frame中的另一个元素匹配,如何替换数据帧第二列中的要素? - How to replace elemets in second column of data frame if the element in first column match another element in another data.frame? 如何删除包含另一个数据框中列的元素的行 - how do I remove rows that contains an element of a column in another data frame 根据元素出现的频率将data.frame行分配给R中的另一个data.frame - Assigning rows of data.frame to another data.frame in R based on frequency of element's occurance
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM