[英]how to conditionally create new column based on the values of a column in one dataframe and the column header names of another dataframe in R
[英]How to match numbers in a column from one dataframe to a column of coarser grained values in another dataframe R
所以我的問題特別是日期,我有一個帶有粗略日期/時間戳(每 30 秒)的 dataframe,每個時間戳也有一個唯一標識符,然后我有一個更長的 dataframe,每 0.5 秒采樣一次,並且我想在更長的 dataframe 中添加一列,說明每個時間戳匹配的第一個 dataframe 中的哪個標識符。
完全忽略日期,這可能很簡單:
a <- data.frame(id = 1:3, value = seq(0,6,3))
b <- c(0.1,1.2,2.4,3.1,4.2,5.3,6.4)
並想要一個 output
1 1 1 2 2 2 3
非常感謝
我們可以使用findInterval
findInterval(b, a$value)
#[1] 1 1 1 2 2 2 3
或cut
cut(b, c(a$value, Inf), labels = FALSE)
#[1] 1 1 1 2 2 2 3
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.