gpt4 book ai didi

R - 根据同一行不同列的值填充缺失值(空白)

转载 作者:行者123 更新时间:2023-12-01 10:41:15 29 4
gpt4 key购买 nike

我正在使用 R 并具有以下数据框示例,其中所有变量都是因子:

  first            second  third
social birth control high
birth control high
medical Anorexia Nervosa low
medical Anorexia Nervosa low
Alcoholism high
family Alcoholism high

基本上,我需要一个函数来帮助我根据第二列和第三列中的值填充第一列中的空白。例如,如果我在第二列中有“节育”,在第三列中有“高”,我需要用“社会”来填充第一列中的空白。如果第二栏和第三栏分别是“Alcoholism”和“high”,我需要用“family”来填第一栏的空白。

最佳答案

根据显示的数据,对于“second”和“third”的每个组合,“first”中是否有其他值不是很清楚。如果只有一个值,您需要用它替换 '',那么您可以尝试

library(data.table)
setDT(df1)[, replace(first, first=='', first[first!='']),
list(second, third)]

或者更有效的方法是

setDT(df1)[, first:= first[first!=''] , list(second, third)]
# first second third
#1: social birth control high
#2: social birth control high
#3: medical Anorexia Nervosa low
#4: medical Anorexia Nervosa low
#5: family Alcoholism high
#6: family Alcoholism high

数据

df1 <- structure(list(first = c("social", "", "medical", "medical", 
"", "family"), second = c("birth control", "birth control",
"Anorexia Nervosa",
"Anorexia Nervosa", "Alcoholism", "Alcoholism"), third = c("high",
"high", "low", "low", "high", "high")), .Names = c("first", "second",
"third"), class = "data.frame", row.names = c(NA, -6L))

关于R - 根据同一行不同列的值填充缺失值(空白),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29981712/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com