[英]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.