Excel 将数组中的分隔字符串值分隔为布尔变量
我有一个数据集,它以字符串数组的形式列出了每天完成的不同活动,类似的概念。正如我在Excel中所做的那样,每个活动都是分隔的,可以很容易地划分为列Excel 将数组中的分隔字符串值分隔为布尔变量,excel,dataset,gretl,Excel,Dataset,Gretl,我有一个数据集,它以字符串数组的形式列出了每天完成的不同活动,类似的概念。正如我在Excel中所做的那样,每个活动都是分隔的,可以很容易地划分为列 activites Work | family | date | gaming | relax | good sleep | shopping Work | family | date | Nature | Crusin | reading | gaming | relax | good sleep | cooking | laundry
activites
Work | family | date | gaming | relax | good sleep | shopping
Work | family | date | Nature | Crusin | reading | gaming | relax | good sleep | cooking | laundry
family | date | movies & tv | gaming | sport | relax | medium sleep | cooking
Work | family | date | Photography | gaming | relax | good sleep | medium sleep | cooking
Work | family | date | Nature | reading | gaming | relax | good sleep | cleaning
我想做的是将每个活动转换成一个布尔变量,该变量有自己的列,因此它表示0
在当天没有完成活动,表示1
完成了活动。它看起来像这样:
Work Family Date Gaming Relax
1 1 0 1 0
1 1 1 0 0
0 0 1 0 1
所以,我最终做的是使用我的Java知识来重新格式化数据。我首先将这些活动划分为各自的变量,每个变量都包含一个数字(二进制)值,以指示当天是否完成了该活动。我不得不分别对待睡眠质量,所以这部分看起来有点不稳定。下面是生成正确输出的代码:
public static void main(String[] args) throws FileNotFoundException {
Scanner scan = new Scanner(new FileReader("activities.txt"));
String[] actList = { "Work", "school", "family", "friends", "date", "nature", "crusin", "photography",
"making music/piano", "movies & tv", "reading", "gaming", "sport", "relax", "sleep", "shopping", "cleaning",
"cooking", "laundry" };
int row = 0;
while (scan.hasNextLine()) {
row++;
int col = 0;
int activityNo = 0;
int[] actValue = new int[actList.length];
String pipeDelim = scan.nextLine();
String[] actName = pipeDelim.split(" \\| ");
int sleepTagsUsed = 0;
while (activityNo < actName.length) {
col = 0;
for (String a : actName) {
if (a.contains("sleep")) {
col = 14;
if (a.equalsIgnoreCase("bad sleep") || a.equalsIgnoreCase("bad sleep\t")) {
if (col < actList.length) {
actValue[col] = 0;
if (sleepTagsUsed == 0) {
col++;
}
sleepTagsUsed++;
} else {
break;
}
if (!(activityNo > actName.length)) {
activityNo++;
}
} else if (a.equalsIgnoreCase("medium sleep") || a.equalsIgnoreCase("medium sleep\t")) {
if (col < actList.length) {
actValue[col] = 1;
if (sleepTagsUsed == 0) {
col++;
}
sleepTagsUsed++;
} else {
break;
}
if (!(activityNo > actName.length)) {
activityNo++;
}
} else if (a.equalsIgnoreCase("good sleep") || a.equalsIgnoreCase("good sleep\t")) {
if (col < actList.length) {
actValue[col] = 2;
if (sleepTagsUsed == 0) {
col++;
}
sleepTagsUsed++;
} else {
break;
}
if (!(activityNo > actName.length)) {
activityNo++;
}
} else if (a.equalsIgnoreCase("sleep early") || a.equalsIgnoreCase("sleep early\t")) {
if (col < actList.length) {
actValue[col] = 3;
if (sleepTagsUsed == 0) {
col++;
}
sleepTagsUsed++;
} else {
break;
}
if (!(activityNo > actName.length)) {
activityNo++;
}
} else {
if (col < actList.length) {
actValue[col] = -1;
} else {
break;
}
System.out.println("No sleep logged error");
}
} else {
int j = 0;
for (String i : actList) {
if (a.equalsIgnoreCase(i) || a.equalsIgnoreCase(i + "\t")) {
actValue[col] = 1;
if (activityNo > actName.length) {
break;
} else {
activityNo++;
break;
}
} else {
if (col < actList.length) {
j++;
if (j > col) {
actValue[col] = 0;
col++;
}
} else {
break;
}
}
}
col++;
}
}
}
for (int p : actValue) {
System.out.print(p + "\t");
}
System.out.println();
}
scan.close();
}
publicstaticvoidmain(字符串[]args)抛出FileNotFoundException{
Scanner scan=新扫描仪(新文件阅读器(“activities.txt”);
String[]actList={“工作”、“学校”、“家庭”、“朋友”、“日期”、“自然”、“克鲁辛”、“摄影”,
“制作音乐/钢琴”、“电影和电视”、“阅读”、“游戏”、“运动”、“放松”、“睡眠”、“购物”、“清洁”,
“烹饪”、“洗衣”};
int行=0;
while(scan.hasNextLine()){
行++;
int col=0;
int activityNo=0;
int[]actValue=新int[actList.length];
String pipeDelim=scan.nextLine();
字符串[]actName=pipeDelim.split(“\\\\”);
int sleepTagsUsed=0;
while(activityNoactName.length)){
activityNo++;
}
}else if(a.equalsIgnoreCase(“中等睡眠”)| | a.equalsIgnoreCase(“中等睡眠”)){
如果(列actName.length)){
activityNo++;
}
}else if(a.equalsIgnoreCase(“良好睡眠”)| | a.equalsIgnoreCase(“良好睡眠”)){
如果(列actName.length)){
activityNo++;
}
}else if(a.equalsIgnoreCase(“sleep early”)| | a.equalsIgnoreCase(“sleep early\t”)){
如果(列actName.length)){
activityNo++;
}
}否则{
如果(列actName.length){
打破
}否则{
activityNo++;
打破
}
}否则{
如果(列col){
actValue[col]=0;
col++;
}
}否则{
打破
}
}
}
col++;
}
}
}
用于(INTP:actValue){