简体   繁体   English

R,使用for循环,如何根据不同的目标数据框生成具有不同结果的新变量

[英]R, using for loop, how can I generate new variables with different results according to different targeted data frame

R, using for loop, how can I generate new variables with different results according to the different targeted data frame. R,使用for循环,如何根据不同的目标数据框生成具有不同结果的新变量。

For example, I have data-frames named as Version1, version2, version3,version4, version5,version6.例如,我有名为 Version1、version2、version3、version4、version5、version6 的数据框。

For detail in versions:有关版本的详细信息:

Version1:版本1:
Name Age HW_score Midterm Final Gender Year_in_school姓名 年龄 HW_score 期中决赛 性别 Year_in_school
AA 21 90 90 90 F 1机管局 21 90 90 90 楼 1
AB 20 80 88 100 F 1 AB 20 80 88 100 楼 1
AC 22 85 95 97 M 1交流电 22 85 95 97 米 1
AD 21 88 99 96 M 1公元 21 88 99 96 米 1

Version2:版本2:
Name Age HW_score Midterm Final Gender Year_in_school姓名 年龄 HW_score 期中决赛 性别 Year_in_school
AE 23 90 70 90 F 1 AE 23 90 70 90 楼 1
BB 20 84 88 90 M 1 BB 20 84 88 90 米 1
DC 21 85 90 97 M 1直流 21 85 90 97 米 1
AD 22 88 94 96 M 2公元 22 88 94 96 米 2

Version3:版本 3:
Name Age HW_score Midterm Final Gender Year_in_school姓名 年龄 HW_score 期中决赛 性别 Year_in_school
AZ 23 90 70 94 F 2亚利桑那州 23 90 70 94 楼 2
CB 20 89 88 90 M 3 CB 20 89 88 90 米 3
FC 21 78 90 97 F 1 FC 21 78 90 97 F 1
AA 24 89 96 96 M 2 AA 24 89 96 96 米 2

similar for version4,5,6版本 4、5、6 类似

If I have to select the row where the age is 21.如果我必须 select 年龄为 21 的行。

Q1: How can I create variables named as result_[the name of the data frame]: result_version1, result_version2, result_version3, result_version4,result_version5,result_version6. Q1:如何创建名为 result_[数据框名称] 的变量:result_version1、result_version2、result_version3、result_version4、result_version5、result_version6。 And store the results of each data frame into the new variables.并将每个数据帧的结果存储到新的变量中。

The desired results are期望的结果是

result_version1:结果_版本1:
Name Age HW_score Midterm Final Gender Year_in_school姓名 年龄 HW_score 期中决赛 性别 Year_in_school
AA 21 90 90 90 F 1机管局 21 90 90 90 楼 1
AD 21 88 99 96 M 1公元 21 88 99 96 米 1

result_version2:结果_版本2:
Name Age HW_score Midterm Final Gender Year_in_school姓名 年龄 HW_score 期中决赛 性别 Year_in_school
DC 21 85 90 97 M 1直流 21 85 90 97 米 1

result_version3:结果_版本3:
FC 21 78 90 97 F 1 FC 21 78 90 97 F 1

similar for result_version4,5,6 result_version4,5,6 类似

Q2: What if storing results from different data frame into one variable: Q2:如果将不同数据帧的结果存储到一个变量中怎么办:

Desired output:所需的 output:

result_version_all result_version_all
Name Age HW_score Midterm Final Gender Year_in_school姓名 年龄 HW_score 期中决赛 性别 Year_in_school
AA 21 90 90 90 F 1机管局 21 90 90 90 楼 1
AD 21 88 99 96 M 1公元 21 88 99 96 米 1
DC 21 85 90 97 M 1直流 21 85 90 97 米 1
FC 21 78 90 97 F 1 FC 21 78 90 97 F 1

How should write the code in R using for loop to have the above output?应该如何使用for循环在R中编写代码以获得上述output? Thanks!!!谢谢!!!

You can put all the dataframes in a list and subset them using lapply .您可以将所有数据框放在一个列表中,并使用lapply它们进行子集化。

result <- lapply(mget(paste0('Version', 1:6)), function(x) subset(x, Age == 21))

result has list of dataframes with output. result具有 output 的数据帧列表。 To get them as separate dataframes use list2env要将它们作为单独的数据框使用list2env

names(result) <- paste0('result_',paste0('Version', 1:6))
list2env(result, .GlobalEnv)

Combine all the list of dataframes into one result.将所有数据框列表合并为一个结果。

result_version_all <- do.call(rbind, result)

You could use the following code, it does not involve a for loop though.您可以使用以下代码,但它不涉及 for 循环。

Q1 result_version1 = Version1[which(Version1$Age == 21),] # repeat for all versions Q1 result_version1 = Version1[which(Version1$Age == 21),] # repeat for all versions

Q2 result_version_all = rbind(result_version1,result_version2,...result_version6) Q2 result_version_all = rbind(result_version1,result_version2,...result_version6)

暂无
暂无

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

相关问题 子集数据帧并使用loop或lapply存储到R中的不同变量 - Subset a data frame and store to different variables in R using loop or lapply 如何使用R中的两个不同变量将新列添加到数据框? - How do you add a new column to a data frame using two different variables in R? R循环根据数据框中的值将文件复制到不同的文件夹中 - R Loop to copy files into different folders according to values in data frame 如何创建 function 以根据 R 中不同 dataframe 中的值生成新变量 - How can I create a function to generate new variables based on values in different dataframe in R 如何在R的循环中创建具有不同y变量的图? - How can I create plots with different y variables in a loop in R? 如何使用循环在 R 中使用不同变量运行回归? - How do I run a regression with different variables in R using a loop? 如何生成具有不同方差(异方差)的随机数据。 在 R - How can I generate random data with different varience (heteroskedasticity). in R 如何根据值列表拆分长字符串中的单词以在 R 中创建新数据框? - How can I split words in a long string according to a list of values to create a new data frame in R? 如何使用不同列的数据框进行循环? - How to for loop using different columns of data frame? 在R data.frame中,如何根据当前已有的变量添加新的变量 - In R data.frame, how to add new variables according to the current existing variables
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM