我正在for loop
中学习R
。我想弄清楚如何在for loop
中的特定variable
中制作data.frame
。我在stackoverflow中查看了几个示例,例如Q1,Q2,Q3,但没有一个对我正在寻找的内容有用!
作为使用mtcars
数据集的示例,假设我要执行for loop
:
查看cyl
并定义cyl 4,6,8,
写入并分别用strings
(low, medium, high
)替换。
for (i in mtcars$cyl) {
if (mtcars$cyl == 4){
print("low")}
if (mtcars$cyl == 6) {
print ("medium")}
if (mtcars$cyl == 8) {
print ("high")}
}
当然不是这样的!
预期是这样的:
mpg cyl disp hp drat wt qsec vs am gear carb
Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4
Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4
Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1
Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1
Hornet Sportabout 18.7 8 360 175 3.15 3.440 17.02 0 0 3 2
Valiant 18.1 6 225 105 2.76 3.460 20.22 1 0 3 1
转换为:
mpg cyl disp hp drat wt qsec vs am gear carb
Mazda RX4 21.0 medium 160 110 3.90 2.620 16.46 0 1 4 4
Mazda RX4 Wag 21.0 medium 160 110 3.90 2.875 17.02 0 1 4 4
Datsun 710 22.8 low 108 93 3.85 2.320 18.61 1 1 4 1
Hornet 4 Drive 21.4 medium 258 110 3.08 3.215 19.44 1 0 3 1
Hornet Sportabout 18.7 high 360 175 3.15 3.440 17.02 0 0 3 2
Valiant 18.1 medium 225 105 2.76 3.460 20.22 1 0 3 1
非常感谢任何有关解释的帮助!
答案 0 :(得分:4)
这种特殊的操作可以在没有循环的情况下进行。
df = mtcars
for (i in 1:length(df$cyl)) { #Iterate over the length of df$cyl
#You could also do "for (i in seq_along(df$cyl)"
#Run "seq_along(df$cyl)" and "1:length(df$cyl)" to understand what values are being generated
if (df$cyl[i] == 4){ #Index df$cyl by [i]. If the ith value is 4, assign 'low'
df$cyl[i] = "low"
}
if (df$cyl[i] == 6) {
df$cyl[i] = "medium"
}
if (df$cyl[i] == 8) {
df$cyl[i] = "high"
}
}
但是对于运行循环,我会这样做
$(document).ready(function() {
$('input:radio[name="meal"]').change(function(ev){
if ($(this).val() == 'Burger with fries') {
var meal = 20;
}
else {
var meal = 30;
}
$('#meal').html((meal).toFixed(2));
updateTotal();
});
function updateTotal(){
var totalAmount = 0.0;
$('.itemAmount').each(function() {
if(isNumeric($(this).text())){
totalAmount = totalAmount + parseFloat($(this).text());
$('#total').text(totalAmount.toFixed(2));
}
});
}
function isNumeric(n) {
return !isNaN(parseFloat(n)) && isFinite(n);
}
$('input:radio[name="drinks"]').change(function(ev){
if ($(this).val() == 'Ice Tea') {
var drinks = 1;
}
else if ($(this).val() == 'Coke') {
var drinks = 1.20;
}
else if ($(this).val() == 'Lemonade') {
var drinks = 2;
}
else if ($(this).val() == 'Water') {
var drinks = 1.75;
}
else if ($(this).val() == 'Hot choco') {
var drinks = 3;
}
else if ($(this).val() == 'Hot coffee') {
var drinks = 1.25;
}
else {
var drinks = 0;
}
$('#drinks').html((drinks).toFixed(2));
updateTotal();
});
$('input:radio[name="extras"]').change(function(ev){
if ($(this).val() == 'Rice') {
var extras = 10;
}
else if ($(this).val() == 'Gravy') {
var extras = 5;
}
else {
var extras = 0;
}
$('#extras').html((extras).toFixed(2));
updateTotal();
});
$('textarea[name="spoon"]').keyup(function(ev){
var spoon = $(this).val().replace(/\n/g, '<br/>');
var spoon = spoon*2;
$("#spoon").html(spoon);
updateTotal();
});
$('input:text[name="fork"]').keyup(function(ev){
var fork = $(this).val().replace(/\n/g, '<br/>');
var fork = fork*1.25;
$("#fork").html(fork);
**updateTotal();**
});
});