如何根据另一个数据框中的数据更改数据框中的某些行在R中?

huangapple go评论64阅读模式
英文:

How can I change some rows in a data frame according to another Data frame data in R?

问题

你的第一个代码中出现了错误,因为在尝试用Mean_glucose$MnStartGlu替换时,出现了"NAs are not allowed in subscripted assignments"错误。这通常是因为在你的match()函数中找不到匹配的时间值,导致了NA值。你可以检查一下Mean_glucose$Time_GlucoseDF$Time_Glucose是否有相同的时间值,并确保它们都没有缺失值。

你的第二个代码中也有错误,因为在使用mutate()函数时出现了"object 'Gluc.Mean_glucose' not found"错误。这可能是因为在select()函数中使用了-Gluc.DFGluc.Mean_glucose,但这些列名可能在数据框中不存在。你可以检查一下数据框中的列名是否正确。

总之,你需要确保两个数据框中的时间值能够正确匹配,且列名和对象名都是正确的。如果问题仍然存在,你可能需要提供更多的代码和数据以便更详细地检查问题。

英文:

I have 2 data frames (DF and Mean_glucose). Mean_glucose includes correct glucose data (Gluc) for a specific time (Time_Glucose). So I should replace these correct data with the data in the DF. The code should find same timing and replace glucose value. For this reason I wrote 2 different codes but both them didn't work.

First one:

 DF$Gluc[match(Mean_glucose$Time_Glucose, DF$Time_Glucose)] <- Mean_glucose$MnStartGlu
 Error in DF$Gluc[match(Mean_glucose$Time_Glucose, DF$Time_Glucose)] <- Mean_glucose$MnStartGlu : 
 NAs are not allowed in subscripted assignments

After this error, I used "na.omit" function to exclude all of NA values but it still didn't work.

Second code:

 left_join(DF,Mean_glucose,"Time_Glucose") %>%
   mutate(Gluc=coalesce(Gluc.Mean_glucose, Gluc.DF)) %>%
   select(-Gluc.DF,Gluc.Mean_glucose)
 Error in `mutate()`:
   ℹ In argument: `Gluc = coalesce(Gluc.Mean_glucose, Gluc.DF)`.
   Caused by error in `list2()`:
   ! object 'Gluc.Mean_glucose' not found
   Run `rlang::last_error()` to see where the error occurred.

What is the wrong?

答案1

得分: 1

I am inferring from your question that the variable Time_Glucose is unique in each data frame and that DF$Mean_Glucose contains all the correct values. Essentially, you just need to copy the values from DF to Mean_Glucose.

如果变量Time_Glucose在每个数据框中都是唯一的,并且DF$Mean_Glucose包含所有正确的值,那么您只需要将值从DF复制到Mean_Glucose中。

If both dataframes are sorted according to their time, you could simply assign the values from Mean_Glucose$Gluc to DF$Gluc, thus essentially overwriting them. If you want to use the tidyverse, you can also use left_join although using the time as unique identifier. You might have to change the variable names slightly.

如果两个数据框都按照时间排序,您可以简单地将Mean_Glucose$Gluc中的值分配给DF$Gluc,从而基本上覆盖它们。如果您想使用tidyverse,您也可以使用left_join,尽管使用时间作为唯一标识符。您可能需要稍微更改变量名称。

英文:

I am infering from your question that the variable Time_Glucose is unique in each data frame and that DF$Mean_Glucose contains all the correct values. Essentially, you just need to copy the values from DF to Mean_Glucose.

If both dataframes are sorted according to their time, you could simply assign the values from Mean_Glucose$Gluc to DF$Gluc, thus essentially overwriting them. If you want to use the tidyverse, you can also use left_join although using the time as unique identifier. You might have to change the variable names slightly.

set.seed(1)

# Set the unique ID in this case time
Time_Glucose <- sample(seq(as.POSIXct('2013/01/01'), as.POSIXct('2017/05/01'), by="15 mins"), 10)

# Generate some random values
Gluc <- rnorm(10, 0, 1)
Gluc2 <- Gluc
Gluc2[c(2,5)] <- c(0.3, 2)

# Create the MRE dataframes
DF <- data.frame(Time_Glucose, Gluc)
Mean_Glucose <- data.frame(Time_Glucose, Gluc2)

# Solution 1: Simple assignment
DF$Gluc <- Mean_Glucose$Gluc2

# Solution 2: Join the data using left_join from the tidyverse with Time_Glucose as the same value
DF <- left_join(Mean_Glucose, DF, by="Time_Glucose")

huangapple
  • 本文由 发表于 2023年5月10日 20:24:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/76218388.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定