我正在RStudio工作。我有一个名为“ X12_5_3”的Excel文件中的数据:
P, 1, 2, 3
5350, 17, 44, 50
25831, 788, 890, 1409
5594, 968, 218, 344
10359, 212, 185, 306
我想创建一个名为weight
的新列,并为前两行添加值0.4
,为后两行添加值0.6
。
我目前的代码是:
data<- X12_5_3
答案 0 :(得分:0)
这是一个建议的解决方案:
按参考设置基本数据框:
X12_5_3 <-data.frame(“ P” = c(5350,25831,5594,10359), “ 1” = c(17,788,968,212), “ 2” = c(44,890,218,185), “ 3” = c(50,1409,344,306))
根据4个值的向量创建数据框
权重<-data.frame(c(0.4,0.4,0.6,0.6))
在数据的最后一列之后将两个数据帧与cbind()
绑定在一起
X12_5_3 <-cbind(X12_5_3,重量)
使用colnames
colnames(X12_5_3)<-c(“ P”,“ 1”,“ 2”,“ 3”,“重量”)
查看更新的数据框:
X12_5_3
这将显示带有添加的列数据的名为X12_5_3
的数据框。
> X12_5_3 <- data.frame( "P" = c(5350, 25831, 5594, 10359),
+ "1" = c(17,788,968,212),
+ "2" = c(44,890,218,185),
+ "3" = c(50, 1409, 344, 306) )
>
> weight <- data.frame( c( 0.4, 0.4, 0.6, 0.6))
>
> X12_5_3 <- cbind(X12_5_3, weight)
> colnames(X12_5_3) <- c("P", "1", "2", "3", "weight")
>
> X12_5_3
P 1 2 3 weight
1 5350 17 44 50 0.4
2 25831 788 890 1409 0.4
3 5594 968 218 344 0.6
4 10359 212 185 306 0.6
答案 1 :(得分:-1)
typedef struct beer_data
{
char name[20]; //names
int id; //ID number given to beer
int quantity; //stock
float price; // pricing
} beer_data;
void search_by_name(beer_data *beers, int total)
{
char buf[20];
printf("Enter name to search: ");
scanf("%19s", buf);
//note, we put %19s because beers[count].name is only 20 bytes long
for(int i = 0; i < total; i++)
{
if(strcmp(beers[i].name, buf) == 0)
{
printf("Found: %s, %d, %d, %.2f\n",
beers[i].name, beers[i].id, beers[i].quantity, beers[i].price);
return;
}
}
printf("%s not found\n", buf);
}
void print_list(beer_data *beers, int total)
{
for(int i = 0; i < total; i++)
{
printf("%s %d %d %.2f\n",
beers[i].name, beers[i].id, beers[i].quantity, beers[i].price);
}
}
void add_item(beer_data *beers, int *total)
{
//note, total has to be passed as pointer
//because we are changing it
//allocate more memory:
beers = realloc(beers, sizeof(beer_data) * (*total + 1));
printf("enter name: ");
scanf("%19s", beers[*total].name);
printf("enter id:");
scanf("%d", &beers[*total].id);
printf("enter quantity:");
scanf("%d", &beers[*total].quantity);
printf("enter price:");
scanf("%f", &beers[*total].price);
//increase the total
*total += 1;
}
int main()
{
FILE *fp = fopen("beer.dat", "r");
if(!fp)
{
printf("Error: can't open file to read\n");
return 0;
}
char buf[500];
int maximum = 0;
fscanf(fp, "%d", &maximum);
//read the rest of the line and discard it
fgets(buf, sizeof(buf), fp);
//allocate memory
beer_data *beers = malloc(maximum * sizeof(beer_data));
int total = 0;
while(1)
{
fgets(buf, sizeof(buf), fp);
sscanf(buf, "%19s", beers[total].name);
if(fscanf(fp, "%d", &beers[total].id) != 1) break;
fgets(buf, sizeof(buf), fp);
if(fscanf(fp, "%d", &beers[total].quantity) != 1) break;
fgets(buf, sizeof(buf), fp);
if(fscanf(fp, "%f", &beers[total].price) != 1) break;
fgets(buf, sizeof(buf), fp);
total++;
if(total == maximum)
break;
}
fclose(fp);
int stop = 0;
while (!stop)
{
printf("\
Enter 0 to exit\n\
Enter 1 print list\n\
Enter 2 for search\n\
Enter 3 add new item\n");
int choice;
scanf("%d", &choice);
switch(choice)
{
case 0:
stop = 1;
break;
case 1:
print_list(beers, total);
break;
case 2:
search_by_name(beers, total);
break;
case 3:
add_item(beers, &total);
break;
}
printf("\n");
}
//cleanup:
free(beers);
return 0;
}