简体   繁体   English

如何基于R中的另一列创建具有多个值的新列

[英]How to create a new column with multiple values based on another column in R

I have a data frame in R called A.Data. 我在R中有一个数据帧,称为A.Data。

It has 8 different columns: plate , row, col , TOF , EXT , green , red , and yellow . 它具有8种不同的列: platerow, colTOFEXTgreenredyellow

Below is an example of the data. 以下是数据示例。

> head(A.Data)
    plate row col TOF EXT green red yellow 
1     1   A  12  20  21     2   0      0      
2     1   C  12  20  17     0   1      0      
3     1   C  11  20  17     0   0      1      
4     1   A  10  20  16     1   1      3      
5     1   A  10  20  16     0   0      0      
6     1   A  10  20  15     0   0      0

I'm trying to add a new column to A.Data called conc (short for concentration). 我试图将新列添加到A.Data称为conc (浓度的缩写)。 The new column called conc depends on the value in the col column. 名为conc的新列取决于col列中的值。

-If col is 1 or 7, conc should equal to 0
-If col is 2 or 8, conc should equal to 0.5
-If col is 3 or 9, conc should equal to 1
-If col is 4 or 10, conc should equal to 2
-If col is 5 or 11, conc should equal to 4
-If col is 6 or 12, conc should say NA

So for the first 6 rows of data, the conc column should say NA, NA, 4, 2, 2, 2 because the col column values for the first 6 rows are 12, 12, 11, 10, 10, 10 . 因此,对于前6行数据,所述conc柱应该说NA, NA, 4, 2, 2, 2 ,因为col用于第一行6列的值是12, 12, 11, 10, 10, 10

I asked my professor for help and he gave me this hint: 我向我的教授寻求帮助,他给了我这个提示:

df$newcol <- rep(1, 1000) will add a new column to the df data frame called newcol and will have 1 replicated 1000 times df$newcol <- rep(1, 1000)将向df数据帧添加一个称为newcol的新列,并将复制1次1000次

Try to add a concentration column called conc with 0, 0.5, 1, 2, 4, NA replicated as many times as you need for the entire column. 尝试添加一个浓度为conc的浓度列,该浓度列根据整个列的需要重复复制0, 0.5, 1, 2, 4, NA

Here is the summary of A.Data$col , in case you might find it useful... 这是A.Data$col的摘要,以防您发现它有用...

> summary (A.Data$col)
   1    2    3    4    5    6    7    8    9   10   11   12 NA's 
1128  703  538  256  156   30 2101 1039  741  294   73   60   11

Thank you! 谢谢!

Not tested, but this may work 未经测试,但这可能有效

map_column <- rep(c(0, 0.5, 1, 2, 4, NA),2)
df$newcol <- map_column[df$col]

EDIT: The idea behind this code is: map_column , which is a vector of length 12, serves here as a map (in the mathematical sense) between the numbers 1 to 12 and the values in the vector. 编辑:此代码背后的想法是: map_column ,它是长度为12的向量,在这里用作数字1到12与向量中的值之间的映射(在数学意义上)。 For instance, 例如,

map_column[[1]]

returns the first element of the vector (0), and 返回向量(0)的第一个元素,并且

map_column[[9]]

returns the 9th element of the vector (1), and so on. 返回向量(1)的第9个元素,依此类推。 Now R vectors have the capability to process several inputs at once, so that 现在,R向量具有一次处理多个输入的能力,因此

map_column[c(1,9)]

returns the corresponding elements ( c(0,1) ) at these positions in one go. 一次性返回这些位置上的对应元素( c(0,1) )。 Note that it is important to use a single square bracket [ instead of [[ here. 请注意,使用单个方括号[而不是[[这里。

This works. 这可行。

convert <- function(number){
  if(number == 1 | number == 7){return(0)}
  if(number == 2 | number == 8){return(.5)}
  if(number == 3 | number == 9){return(1)}
  if(number == 4 | number == 10){return(2)}
  if(number == 5 | number == 11){return(4)}
  if(number == 6 | number == 12){return(NA)}
}

A.Data$newcol <- do.call(rbind, lapply(A.Data$col, convert))

Use merge. 使用合并。

augment <- data.frame(col=1:12,conc=rep(c(0, 0.5, 1, 2, 4, NA),2))
A.Data  <-merge(A.Data,augment,by="col",sort=F)
A.Data
#   col plate row TOF EXT green red yellow conc
# 1  12     1   A  20  21     2   0      0   NA
# 2  12     1   C  20  17     0   1      0   NA
# 3  11     1   C  20  17     0   0      1    4
# 4  10     1   A  20  16     1   1      3    2
# 5  10     1   A  20  16     0   0      0    2
# 6  10     1   A  20  15     0   0      0    2

This creates an an augment dataframe with 2 columns, col corresponding to col in A.Data , and conc with the augment. 这产生与2列中,一个数据帧扩充col对应于colA.Data ,和conc与扩充。 Then merge that with A.Data based on col. 然后将其与基于col的A.Data合并。

Here's a very different approach based on mathematical and logical operations: 这是一种基于数学和逻辑运算的非常不同的方法:

x <- c(1:12, NA) # an example vector including all possible values

floor(2 ^ (z <- x %% 6 - 2)) / 2 * (z + 2 | NA)

The result: 结果:

[1] 0.0 0.5 1.0 2.0 4.0  NA 0.0 0.5 1.0 2.0 4.0  NA  NA

(I fear that this solution may appear like obfuscation.) (我担心这种解决方案可能看起来像是迷惑。)

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

相关问题 根据另一列(在 R 中)中的值创建一个新列 - Create a new column based on the values in another column (in R) 如何基于一个数据框中的列的值和R中另一个数据框的列标题名称有条件地创建新列 - 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 R:如何创建一个基于另一列某些值的新列? - R: How to create a new column with values based on certain values of another column? 根据R中的另一列创建一个新列 - Create a new column based on another column in R 如何根据R中的另一列内容创建新列 - How to create new column based on another column's contents in R 根据R中列的值创建另一列 - create another column based on the values on a column in R 根据另一列中的4个值创建新列 - Create new column based on 4 values in another column 如何根据条件创建与另一列具有相同值的新列? - How to create a new column with the same values of another column based on a condition? R:从多列创建基于新列的值列表 - R: Create new column based list of values from a multiple columns 基于 R 中另一列中的唯一值创建升序值列,并批量添加新数据 - Create column of ascending values based on unique values in another column in R with new data added in batches
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM