简体   繁体   English

R-根据另一个条件填充 dataframe

[英]R- Populating a dataframe based on another one with conditions

im new on R and i have a data set of 22x252, the 252 have many repeated values on column 1(ID).我是 R 的新手,我有一个 22x252 的数据集,252 在第 1 列(ID)上有许多重复值。 I made another dataset that has nrows of the unique values (with those values already populated), and i want to populate the rest of the columns based on the other dataset (basically summing all the values that share the same value in column 1.)我制作了另一个具有 nrows 唯一值的数据集(已经填充了这些值),并且我想根据另一个数据集填充列的 rest(基本上将在第 1 列中共享相同值的所有值相加。)

Is there a basic function that enables me to do this?是否有基本的 function 使我能够做到这一点?

Thanks & Regards感谢和问候

We can use aggregate in base R .我们可以在base R中使用aggregate Assuming the column name of first column is 'ID' and all other columns are numeric class, we group by 'ID' and get the sum of the rest of the columns in aggregate假设第一列的列名是“ID”,所有其他列都是数字 class,我们按“ID”分组并得到aggregate列的 rest 的sum

aggregate(.~ ID, df1, sum, na.rm = TRUE)

Or with dplyr或与dplyr

library(dplyr)
df1 %>%
  group_by(ID) %>%
  summarise_at(vars(-group_cols()), sum, na.rm = TRUE)

Or with new version with across或与新版本across

df1 %>%
  group_by(ID) %>%
  summarise(across(-group_cols(), sum, na.rm = TRUE))

暂无
暂无

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

相关问题 R-基于另一个数据框中的值在一个数据框中创建值的问题 - R- Issue with creating values in a dataframe based on values in another dataframe R-基于2个变量对数据帧进行子集化(其中一个是随机数,以便对第一个变量进行采样)? - R- Subsetting dataframe based on 2 variables (where one is a random number in order to sample the first variable)? 根据条件用另一个数据框替换数据框列 - R - Replace Dataframe column with another dataframe based on conditions - R 根据另一个数据帧中的匹配条件将列添加到 R 中的数据帧 - Adding column to a dataframe in R based on matching conditions in another dataframe R循环遍历数据框列中的唯一值以根据条件创建另一个 - R Loop over unique values in a dataframe column to create another one based on conditions 将新列添加到一个 Dataframe 基于另一个 R 中的条件的值和函数 - Add New Columns to One Dataframe Based on Values and Functions in Another With Conditions in R 根据 R 中的两个匹配条件将一个数据帧中的值添加到另一个数据帧 - Adding values from one dataframe to another based on two matching conditions in R R- 在每一行数据帧之后插入另一个数据帧的行? - R- Insert rows of another dataframe after every row of dataframe? R-根据另一个文件中的数据有条件地从一个文件中提取数据 - R- Conditionally extracting data from one file based on data in another file R- Dataframe 从旧的 dataframe - R- Dataframe making a new dataframe from a old one
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM