简体   繁体   English

根据条件将值分配给另一列中的一列

[英]Assign value to a column from another column based on condition

Say that I have a list like this: 说我有一个这样的清单:

> desired <- c("10001", "10004")

And a sample data frame like this: 还有一个示例数据框,如下所示:

> desired_sample_df <- data.frame(geo = rep("other", 30), zip = c(rep(10001:10010, 2), 10011:10020), cbsa = c(rep("NY", 20), rep("CA", 10)))
> desired_sample_df
     geo   zip cbsa
1  other 10001   NY
2  other 10002   NY
3  other 10003   NY
4  other 10004   NY
5  other 10005   NY
6  other 10006   NY
7  other 10007   NY
8  other 10008   NY
9  other 10009   NY
10 other 10010   NY
11 other 10001   NY
12 other 10002   NY
13 other 10003   NY
14 other 10004   NY
15 other 10005   NY
16 other 10006   NY
17 other 10007   NY
18 other 10008   NY
19 other 10009   NY
20 other 10010   NY
21 other 10011   CA
22 other 10012   CA
23 other 10013   CA
24 other 10014   CA
25 other 10015   CA
26 other 10016   CA
27 other 10017   CA
28 other 10018   CA
29 other 10019   CA
30 other 10020   CA

I would like to overwrite the geo column with a value from zip only if the value of zip is in the desired list saved at the start. 仅当zip的值在开始时保存的desired列表中时,我才想用zip的值覆盖geo列。


Here is what I've tried: 这是我尝试过的:

> desired_sample_df$geo[desired_sample_df$zip %in% desired] <- desired_sample_df$zip[which(desired_sample_df$zip %in% desired)]
Warning message:
In `[<-.factor`(`*tmp*`, desired_sample_df$zip %in% desired, value = c(NA,  :
  invalid factor level, NA generated


> desired_sample_df$geo[desired_sample_df$zip %in% desired] <- desired_sample_df$zip
Warning messages:
1: In `[<-.factor`(`*tmp*`, desired_sample_df$zip %in% desired, value = c(NA,  :
  invalid factor level, NA generated
2: In `[<-.factor`(`*tmp*`, desired_sample_df$zip %in% desired, value = c(NA,  :
  number of items to replace is not a multiple of replacement length

One of the problems is that strings in dataframes automatically become factors. 问题之一是数据帧中的字符串会自动成为因素。 Try this: 尝试这个:

desired <- c("10001", "10004")
df <- data.frame(geo = rep("other", 30), zip = c(rep(10001:10010, 2), 10011:10020), cbsa = c(rep("NY", 20), rep("CA", 10)), stringsAsFactors=FALSE)

idx <- df$zip %in% desired

Now you can alter the elements you want by 现在,您可以通过以下方式更改所需的元素

df[idx, ]$geo <- df[idx, ]$zip

Like this? 像这样?

df$geo <- ifelse(df$zip %in% desired,df$zip,df$geo)

where I'm calling your desired_sample_df , just df . 在这里我叫你的desired_sample_df ,只是df

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 根据列中的条件为组分配值(从配置表中提取) - Assign value (stemming from configuration table) to group based on condition in column 从基于 R 中另一列中的条件的列中获取值? - Obtain value from a column based off condition in another column in R? 根据列中的条件将值分配给组 - Assign value to group based on condition in column 如何根据另一列是否满足 R 中的条件为列分配字符值? - How can I assign a column a character Value based on whether another column meets a condition in R? R:根据条件(不同大小的数据框)从另一个数据框的列中为列分配值 - R: Assign values to column, from a column from another data frame, based on a condition (different sized data frames) 根据从另一个数据框中的ID中选择的值分配新列 - Assign new column based on a value chosen from an id in another dataframe 如何根据R中的另一个列值为列分配值? - How to assign a value for a column based on another column value in R? 根据条件/另一列编辑字符串值? - Edit string value based on a condition / another column? "根据 R 中另一列的条件求和(或不求和)一个值" - Sum (or not) a value based on condition of another column in R 根据第三列上的条件,将数据框中的值替换为另一列中的值 - Replace a value in a dataframe with a value in another column, based on a condition on a third column
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM